Spawn multiple threads dynamically from MFC

Aug 10, 2010 at 8:11am
Hi,

I have a need to start worker threads dynamically every time the user clicks on one of the buttons of my MFC Modal dialog box. The threads in turn, will then change some aspects of the dialog.

As per my knowledge, I have options of using AfxBeginThread or CWinThread::CreateThread. However, I am unable to understand that how will I allocate a distinct thread controlling function dynamically, everytime?

Pls help me out, and let me know if I am thinking in the right direction.

Thanks,
Aug 10, 2010 at 10:36am
Don't you spawn a thread each time the button handler's called?
Aug 10, 2010 at 1:16pm
That's exactly what I am planning to do (i.e spawn a thread each time the button handler's called).
But I don't know how to do it. How to manage the thread controlling function , each time I spawn a new thread?

Thanks,
Aug 10, 2010 at 2:28pm
I'm not sure what you mean. Shouldn't the thread do some work and terminate or do you have to keep talking to it (them)?
Aug 11, 2010 at 7:14am
Hi,

Pls refer to the below Win32 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#define THREADCOUNT 4 
int main() 
{ 
   DWORD IDThread; 
   HANDLE hThread[THREADCOUNT]; //Handle maximum of 4 threads	
   int i; 
 
// Create multiple threads. 
    for (i = 0; i < THREADCOUNT; i++) 
   { 
      hThread[i] = CreateThread(NULL, // default security attributes 
         0,                           // use default stack size 
         (LPTHREAD_START_ROUTINE) ThreadFunc, // thread function 
         NULL,                    // no thread function argument 
         0,                       // use default creation flags 
         &IDThread);              // returns thread identifier 
 
   // Check the return value for success. 
      if (hThread[i] == NULL) 
         ErrorExit("CreateThread error\n"); 
   } 
 
   for (i = 0; i < THREADCOUNT; i++) 
      WaitForSingleObject(hThread[i], INFINITE); 
 
   return 0; 
} 


Here I can create 4 threads simultaneously, but to my knowledge the thread controlling function ("ThreadFunc") will be common to all the 4 threads. Now if one of the threads becomes signaled (for example), then how do I process it inside the ("ThreadFunc") ?

Hope I was able to express myself this time (-:

Thanks,
Aug 11, 2010 at 8:09am
A thread is signalled when it terminates. Unitl then, you may assume it's running.

In your example, you start four threads running function ThreadFunc. That means, ThreadFunc function is being executed in four threads of execution at the same time.

At the end you wait for each one to complete. As an alternative to looping with WaitForSingleObject, you can use WaitForMultipleObjects which can wait for upto 64 things at a time.

You also need to close each thread handle when you're done.
Aug 11, 2010 at 10:45am
Thanks, your reply gives me some idea of what I should do.

Also, when using WaitForMultipleObjects, is it possible to take individual actions when WaitForMultipleObjects for each thread returns?

I mean, suppose WaitForMultipleObjects for the 3rd thread returns, while the other threads are still continuing. How to handle this situation in code, assuming that all the above scenarios are occurring dynamically, at program runtime?

Thanks,
Aug 11, 2010 at 10:53am
is it possible to take individual actions when WaitForMultipleObjects for each thread returns?
Yes. WaitForMultipleObjects can return when one object signals or when they all do.

The original question suggested you wanted to spawn threads to do some suff and terminate on their own. If this is the case, and you don't really care when they complete. you don't have to wait for them to complete. Just close the handle once you've created the threads and forget about them.
Topic archived. No new replies allowed.