Click to See Complete Forum and Search --> : Threads : Total Confusion


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.

Igor Vartanov
June 29th, 2005, 12:32 PM
Try to see this thread (http://www.codeguru.com/forum/showthread.php?p=1119212#post1119212). It is not exactly what you wanted but somehow is touches the questions you asked.

My question is - why do you "need lots of these threads?" Either you need some limited (not very big) number of threads or you have bad application design - in most cases this rule works fine.

If you have any questions about threads - ask, but be not so... m-m-m... belletristic. :)

BTW, good books on MFC help a lot... Usually.

Andreas Masur
June 29th, 2005, 12:42 PM
[ Redirected thread ]

Andreas Masur
June 29th, 2005, 12:43 PM
MSDN:

Multithreading Topics (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_multithreading_topics.asp)
Multiple Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/multiple_threads.asp)
Multithreading for Rookies (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_threads.asp)

FAQs:

How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?t=312452)
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
How to use member functions as thread functions? (http://www.codeguru.com/forum/showthread.php?t=312453)
How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

RogerGarrett
June 30th, 2005, 09:27 AM
I've read through all of the recommended articles and I still do not have an answer to part of my question:

If I use the User-Interface form of a thread it means that I create a CWinThread-derived class that implements the thread processing that I want, and then I pass the class of that CWinThread-derived class to the AfxBeginThread. All fine. I understand that much. But what I don't understand, and what I have been unable to come across in all the literature, is where exactly within my CWinThread-derived class do I put my code to do the processing that I want to do?

In one example I came across the programmer put it into the InitInstance method. That's clearly not correct. In another example the programmer put the processing code into the Run method. That seems more reasonable, but then the Microsoft documentation for the Run method states that "Run is rarely overridden", indicating that that's not the right place.

So, my question:

Where within my CWinDerived class do I put the code that actually performs my processing for the thread?

MrViggy
June 30th, 2005, 11:54 AM
I've read through all of the recommended articles and I still do not have an answer to part of my question:

If I use the User-Interface form of a thread it means that I create a CWinThread-derived class that implements the thread processing that I want, and then I pass the class of that CWinThread-derived class to the AfxBeginThread. All fine. I understand that much. But what I don't understand, and what I have been unable to come across in all the literature, is where exactly within my CWinThread-derived class do I put my code to do the processing that I want to do?

In one example I came across the programmer put it into the InitInstance method. That's clearly not correct. In another example the programmer put the processing code into the Run method. That seems more reasonable, but then the Microsoft documentation for the Run method states that "Run is rarely overridden", indicating that that's not the right place.

So, my question:

Where within my CWinDerived class do I put the code that actually performs my processing for the thread?
If you are creating a user interface thread, then your code goes into messages that will be passed to your thread (kinda like a dialog class). So, you can define your own messages; add functions, and the messages to the message map of your thread class; then outside your thread, you call PostThreadMessage (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/postthreadmessage.asp) with your user defined message.

If all you want is a worker thread, then see the link that Andreas Masur posted about creating a worker thread.

Viggy