Click to See Complete Forum and Search --> : Is a Sleep() needed after CreateThread() ?
hexor47
May 23rd, 2010, 09:24 AM
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.
hoxsiew
May 23rd, 2010, 09:49 AM
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:
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
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;
}
hexor47
May 23rd, 2010, 10:18 AM
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 :
int param=666;
while (something) {
HANDLE hThread;
hThread=CreateThread(NULL, 0, threadedFunction, (LPVOID)¶m, 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.
}
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()...
hoxsiew
May 23rd, 2010, 11:57 AM
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.
cosmicvoid
May 23rd, 2010, 02:38 PM
int param=666;
while (something) {
HANDLE hThread;
hThread=CreateThread(NULL, 0, threadedFunction, (LPVOID)¶m, 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%.
hexor47
May 23rd, 2010, 07:43 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.