CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Spawn multiple THREADS simultaneously

    Pls refer to the below Win32 code:

    Code:
    #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 4 threads becomes signaled (for example), then how do I process it inside the ("ThreadFunc") ?

    Thanks,

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Spawn multiple THREADS simultaneously

    Every thread has its own stack memory, and thus, its own local variables. So despite the fact that the thread procedure code is the same, every thread lives its own life.
    Best regards,
    Igor

  3. #3
    Join Date
    Aug 2010
    Posts
    2

    Re: Spawn multiple THREADS simultaneously

    Thanks for the reply.

    What I am unable to figure out is that after the thread creation, what source code should be used inside the thread control function, for a particular thread getting signaled?

    For example, if thread #3 was blocked earlier, and now gets signaled, what code should I use in the common thread control function to process thread #3?

    Thanks,

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Spawn multiple THREADS simultaneously

    Frankly, I have no idea what you're talking about. "Blocked", "signaled", "to process"? All this sounds either ambiguous or senseless. Blocked which way? To process thread, what would that mean? Signaled thread? Thread object gets signaled right after thread execution was finished. Now you see? You have to come up with somewhat better explanation of what you want.
    Best regards,
    Igor

  5. #5
    Join Date
    Aug 2010
    Posts
    51

    Cool Re: Spawn multiple THREADS simultaneously

    hi,


    every thread has its own stack memory, and thus, its own local variables. So despite the fact that the thread procedure code is the same, every thread lives its own life.To process thread, what would that mean? Signaled thread? Thread object gets signaled right after thread execution was finished.

    thanks
    regards,
    phe9oxis,
    http://www.guidebuddha.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured