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

    Is a Sleep() needed after CreateThread() ?

    Hello,

    I have some problems with the CreateThread() function. To make a long story short, let's say I have this in a main function :

    int param=1;
    CreateThread(NULL, 0, threadedFunction, (LPVOID)&param, 0, &IDThread);
    param=2;

    and this in the threadedFunction(LPVOID parameter) :

    int *paramReceived=(int*)parameter;
    printf("The param received in the thread is : %d", *paramReceived);

    The result in the console is : sometimes "1" is printed, sometimes it's "2" !!!

    Why do I think a Sleep() could be needed after a CreateThread ?
    Well, I tried to use the WaitForSingleObject just after the CreateThread and BEFORE the "param=2;".
    Unfortunately the result is the same : it looks like at the time WaitForSingleObject is called, the thread is not started yet, that's why WaitForSingleObject returns directly and doesn't wait at all.

    So, it seems the problem is solved when I add a Sleep(100) right after CreateThread(). But I find that too much awful !
    I would like to have some advices.

    Thanks.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Is a Sleep() needed after CreateThread() ?

    This is essentially a classic thread synchronization issue. Both your main thread and your worker thread are using the same data and must take precautions to not step on each other's data. Why are you changing param anyway? You should probably copy it to new pointer and pass it to the thread, then let the thread clean up when it is done:

    Code:
      int param=1;
      int *pParam1;
    
      pParam1=new int;
      *pParam1=param;
      
      CreateThread(NULL, 0, threadedFunction, (LPVOID)pParam1, 0, &IDThread);
      param=2;
      //do whatever you want with it.  Thread is using a copy
    Code:
    DWORD WINAPI threadedFunction(LPVOID parameter)
    {
      int *paramReceived=(int*)parameter;
      printf("The param received in the thread is : %d", *paramReceived);
      //do whatever else you want to in the thread
      //...
    
      //cleanup
      delete paramReceived;
      return 1;
    }

  3. #3
    Join Date
    May 2010
    Posts
    8

    Re: Is a Sleep() needed after CreateThread() ?

    Thanks for answering.

    What's your telling me is right and works.
    But in fact my code is a little more complicated :
    the whole CreateThread thing is in a while, and I need to send param to the threadedFunction, let the threadedFunction modify it (it's a pointer), then get the new value of param in the main function :

    Code:
    int param=666;
    while (something) {
      HANDLE hThread;
      hThread=CreateThread(NULL, 0, threadedFunction, (LPVOID)&param, 0, &IDThread);
      WaitForSingleObject(hThread, INFINITE); // I want to wait the threadFunction to terminate
      param=param*10; // Take the param value (supposedly modified by the threadFunction)
                      // and apply a dumb (to simplify the code) operation.
    }
    Code:
    DWORD WINAPI threadedFunction(LPVOID parameter) {
      int *paramReceived=(int*)parameter;
      *paramReceived++; // Dumb operation
    }
    I really simplified the code to point straight at the problem (I understand that it looks totally stupid and useless, but you get the idea).

    Obviously the wait function doesn't wait since the thread isn't started at the moment. So, param's value after the CreateThread() is the same as before the CreateThread()...

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Is a Sleep() needed after CreateThread() ?

    If you have to share the value between the main thread and the worker thread(s) then you will need to synchronize access to the shared variable.

    Do a google search on "c++ read write lock" and you'll find some good resources.

  5. #5
    Join Date
    Apr 2010
    Location
    Western WA, USA
    Posts
    59

    Re: Is a Sleep() needed after CreateThread() ?

    Quote Originally Posted by hexor47 View Post
    Code:
    int param=666;
    while (something) {
      HANDLE hThread;
      hThread=CreateThread(NULL, 0, threadedFunction, (LPVOID)&param, 0, &IDThread);
      while (param == 666) {Sleep(0);}   // wait for thread to modify param
      WaitForSingleObject(hThread, INFINITE); // I want to wait the threadFunction to terminate
    Not too elegant, but it will do what you are asking. The Sleep(0) is advised to prevent tying up the cpu at 100%.
    Last edited by cosmicvoid; May 23rd, 2010 at 02:46 PM.

  6. #6
    Join Date
    May 2010
    Posts
    8

    Re: Is a Sleep() needed after CreateThread() ?

    Hmmm.. it appears the problem come from strings...
    I don't understand why yet, but I have something like that :
    str1=str2;
    and it makes the two string objects sharing the same char[] (the value of the pointer str1.c_str() is equal to the value of the pointer str2.c_str())
    well, there is something I don't get.

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