|
-
May 23rd, 2010, 09:24 AM
#1
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)¶m, 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|