Friday, 6 September 2013

Windows Threading - debug works release does not - attempting to get value from thread when it starts

Windows Threading - debug works release does not - attempting to get value
from thread when it starts

I am attempting to get the ID of a thread for storage in a list of threads.
To do this I am launching a thread with a pointer to a long that will
store the thread ID. The thread ID should be stored as soon as the thread
function executes. The launching function should be able to continue once
the ID is stored.
This code only seems to work in Debug mode but hangs in Release mode. I am
using Visual C++ 2008 Express.
I need the code to work on Windows XP so unfortunately I cannot simply use
GetThreadId as it is only supported on Windows Server 2003 and newer.
thread_wrapper* spawn_thread(void *entryPoint, void *params)
{
thread_wrapper *newThread = calloc(1, sizeof(thread_wrapper));
_beginthread(spawned_thread_wrapper_func, 0, &newThread->_win32ThreadID);
while (newThread->_win32ThreadID == 0) ; // Hangs here in Release mode
... // Safely add thread to list using critical section
return newThread;
}
void spawned_thread_wrapper_func(void *params)
{
long *outThreadIDPtr = (long *)params;
*outThreadIDPtr = GetCurrentThreadId();
// spawn_thread function should now be able to add thread to list
// but still hangs on while waiting loop in Release mode.
// Works fine in Debug mode.
...
}
What is going wrong here?

No comments:

Post a Comment