|
-
January 29th, 2010, 12:49 AM
#1
Object in its own thread?
Hi,
I'm new to this forum and to thread programming.
Been wondering if it's possible for an object to exist in its own thread, so that different instances can run their various methods in their own thread space.
The root of my problem is this:
I've got a few pieces of the same hardware, and I created a class to represent it. Because of how the hardware works, I've got to poll after sending a command to it (by calling an API function) and wait for completion. Now, the different pieces can work simultaneously, but I can't send two commands to the same piece at once.
By running everything in one (main) thread, different pieces of hardware hold each other up.
Code:
int main()
{
myObj obj1, obj2;
// need the 2 objects to execute almost simultaneously
// at the moment, obj2 will only execute after obj1 is done with its job
obj1.doSomething();
obj2.doSomething();
// again, 2 objects to execute simultaneously, upon completion of the previous commands
obj1.doSomethingElse();
obj2.doSomethingElse();
return 0;
}
I've been reading up about the thread object wrapper, but it seems like I need to re-wrap every single method within my custom object.
There's also the singleton pattern, but I'm still reading up on that.
Can anyone point me in the correct direction?
Last edited by radioactive28; January 31st, 2010 at 11:55 PM.
-
January 29th, 2010, 11:26 AM
#2
Re: Object in its own thread?
A class instance can certainly start it's own thread and pass instance data to the thread. Is that what you are asking?
If you turn on viewing signature lines in the CG control panel, you'll see that I've written several multithreading articles. You might want to check them out as I generally always encapsulate threading functionality in a class. The articles have several projects that illustrate this.
-
January 31st, 2010, 08:30 PM
#3
Re: Object in its own thread?
Thanks Arjay, those articles were some of top results when I started searching.
I'm aware that a class can start its own thread - I've managed to do that already, but it looks like I'd have to create an extra member function for each one I want to thread, using the static member function method.
I'm wondering if an object can instantialise in its own thread, so that every member function that I call using the . or -> operator will run in the thread exclusive to the object, wherever I may call it. The implication is that the thread as exists for as long as the object does, instead of the other way round.
Or to look at it another way, multiple commands to the same object will be queued, but multiple commands to different objects will execute.
Sorry if I sound confused, but I'm really still figuring out what problem I'm trying to find a solution for.
-
January 31st, 2010, 11:24 PM
#4
Re: Object in its own thread?
I don't understand what you are trying to do. Can you give us a pseudo code snippet?
-
February 1st, 2010, 12:44 AM
#5
Re: Object in its own thread?
Bear with me on this long-winded one.
What I have currently:
Code:
int main()
{
myObj obj1, obj2;
// need the 2 objects to execute almost simultaneously
obj1.doSomething();
obj2.doSomething();
// again, 2 objects to execute simultaneously, upon completion of the previous commands
obj1.doSomethingElse();
obj2.doSomethingElse();
return 0;
}
class myObj
{
void doSomething() {
// send command to hardware
// poll/wait for completion
}
void doSomethingElse() {
// send different command to hardware
// poll/wait for completion
}
}
The commands sent to the hardware makes it do something which takes a variable amount of time. However, the hardware API does not wait for completion, so I have to poll it to check for completion.
The problem is, if I call obj1.doSomething() and obj2.doSomething() in a single thread, obj2.doSomething() has to wait for obj1.doSomething() to return. This is not ideal, obj1 and obj2 being independent hardware that can work simultaneously.
So I try to implement threading within the class:
Code:
class myObj
{
void doSomething() {
CreateThread(0, 0, doSmthngThread, lpParam, 0, NULL);
}
void doSomethingElse() {
CreateThread(0, 0, doSmthngElseThread, lpParam, 0, NULL);
}
static DWORD WINAPI doSmthngThread(LPVOID lpParam) {
// send command to hardware
// poll/wait for completion
}
static DWORD WINAPI doSmthngElseThread(LPVOID lpParam) {
// send different command to hardware
// poll/wait for completion
}
}
This works, but it means I have to create 2 member functions for each command to the hardware. I suppose this is manageable if the command set for the hardware is small, but it becomes more tedious if I had, say, 30 commands. The problem is made worse as different commands take different arguments, and I'd have to define almost as many structs to pass the arguments.
To really simplify my query, can I achieve the same thing, without creating so many member functions?
Last edited by radioactive28; February 1st, 2010 at 12:47 AM.
-
February 1st, 2010, 01:17 AM
#6
Re: Object in its own thread?
 Originally Posted by radioactive28
To really simplify my query, can I achieve the same thing, without creating so many member functions?
Sure. First of all, create the thread with _beginthreadex instead of CreateThread. Next, create a static thread proc as the thread proc and _beginthreadex, pass in the 'this' pointer to the class. You can see how in my thread articles (all of them use the same technique).
So by passing in the 'this' param to _beginthreadex, this gives the thread access to the class instance members. There's no need to pass in different structures to the thread because passing 'this' in covers all the variables.
Of course, I don't know how complicated you need to get, perhaps creating a base class that contains the thread creation/management code would be a better approach, and deriving classes based on the required command set to be sent to the hardware would be a better design.
However, if the command sets aren't much different between the hardware, you might get by with putting it all in one class.
Another approach is to take the design pattern approach, and create the base class but 'feed' it the appropriate command set class.
-
February 2nd, 2010, 12:12 AM
#7
Re: Object in its own thread?
Thanks for showing the way, think I have a clearer idea on how to move forward now =)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|