RogerGarrett
June 29th, 2005, 12:16 PM
I am Soooooo confused about threads. And, yes, I first did a search on CodeGuru and read all sorts of postings and Windows documentation about threading, but I'm still left utterly confused.
I think that what I want is a "worker" thread, since I don't require it to do any user interface work (hence not a User-Interface thread).
And I'm going to need lots of these threads (all the same kind) all running at the same time. So I'm thinking I need a CWinThread-derived class to handle it.
But a "worker" thread is started up by passing, to the AfxBeginThread method, a pointer to the "controlling function". That would appear to imply that I then have to instantiate an object of my CWinThread-derived class and pass to the AfxBeginThread something like &MyCWinThreadDerivedClassObject.theControllingFunction. That looks odd to me because I don't see, when theControllingFunction returns and the thread thereby terminates, how the MyCWinDerivedClassObject gets deleted.
All of the examples I've come across that implement worker type threads do NOT appear to use a separate class for the thread, but rather merely pass the address of some method within the main program, so there's no class object to delete.
If I instead use a User-Interface type of thread, which merely requires the passing of the "Class" of my CWinThreadDerivedClass, it appears to me that the AfxBeginTread implicitly creates the necessary CWinThreadDerivedClass object and also deletes it when the thread exits.
I guess my questions are these:
1) If I use a WORKER thread, and want to have it handled by a CWinThread-derived class, do I have to instantiate a separate object for each thread (passing the address of some "ProcessTheThread" method within the object) and also maintain some kind of list of all such objects so that I can delete each one as appropriate when the thread processing terminates?
2) If instead I use the User-Interface type of thread, in which case I pass just the class name to the AfxBeginThread, what function within that class actually gets started up by the thread to do the processing. According to documentation on CWinThread there's an overridable InitInstance, but that's just for initially setting things up. There's also an overridable "Run" method but the documentation says that that is "rarely overridden". So, what method within the class do I override in order to perform my processing?
Sign me,...
Utterly Confused.
I think that what I want is a "worker" thread, since I don't require it to do any user interface work (hence not a User-Interface thread).
And I'm going to need lots of these threads (all the same kind) all running at the same time. So I'm thinking I need a CWinThread-derived class to handle it.
But a "worker" thread is started up by passing, to the AfxBeginThread method, a pointer to the "controlling function". That would appear to imply that I then have to instantiate an object of my CWinThread-derived class and pass to the AfxBeginThread something like &MyCWinThreadDerivedClassObject.theControllingFunction. That looks odd to me because I don't see, when theControllingFunction returns and the thread thereby terminates, how the MyCWinDerivedClassObject gets deleted.
All of the examples I've come across that implement worker type threads do NOT appear to use a separate class for the thread, but rather merely pass the address of some method within the main program, so there's no class object to delete.
If I instead use a User-Interface type of thread, which merely requires the passing of the "Class" of my CWinThreadDerivedClass, it appears to me that the AfxBeginTread implicitly creates the necessary CWinThreadDerivedClass object and also deletes it when the thread exits.
I guess my questions are these:
1) If I use a WORKER thread, and want to have it handled by a CWinThread-derived class, do I have to instantiate a separate object for each thread (passing the address of some "ProcessTheThread" method within the object) and also maintain some kind of list of all such objects so that I can delete each one as appropriate when the thread processing terminates?
2) If instead I use the User-Interface type of thread, in which case I pass just the class name to the AfxBeginThread, what function within that class actually gets started up by the thread to do the processing. According to documentation on CWinThread there's an overridable InitInstance, but that's just for initially setting things up. There's also an overridable "Run" method but the documentation says that that is "rarely overridden". So, what method within the class do I override in order to perform my processing?
Sign me,...
Utterly Confused.