,Thread and Mutex
-
Hi everyone, I have a thread. The function that keeps this thread alive contains a while loop. The while loop is true while a boolean variable is maintained true. When the thread has to terminate, the boolean value is modified to false and the thread dies. This boolean variable is global. I am using a Mutex to control access to this global value. I would like to know if it is necessary to control global variables with a Mutex when one is just reading the value? Or does one only control the global variable when one is going to modify its value? This one depends on the previous question. How does one manage something like: while ( g_bGlobal_Variable ) { } How does one apply a mutex object to the above case. Because I cannot do dwWait = ::WaitForSingleObject(); if ( dwWait == 0 ) { while ( g_bGlobal_Variable ) { } } Otherwise this implies that the thread always has control over the Mutex object while alive. Thanks and excuse the ignorance Regards
-
Hi everyone, I have a thread. The function that keeps this thread alive contains a while loop. The while loop is true while a boolean variable is maintained true. When the thread has to terminate, the boolean value is modified to false and the thread dies. This boolean variable is global. I am using a Mutex to control access to this global value. I would like to know if it is necessary to control global variables with a Mutex when one is just reading the value? Or does one only control the global variable when one is going to modify its value? This one depends on the previous question. How does one manage something like: while ( g_bGlobal_Variable ) { } How does one apply a mutex object to the above case. Because I cannot do dwWait = ::WaitForSingleObject(); if ( dwWait == 0 ) { while ( g_bGlobal_Variable ) { } } Otherwise this implies that the thread always has control over the Mutex object while alive. Thanks and excuse the ignorance Regards
First, if you're going to use a synhcronization object to protect a flag, use the synchronization object as the flag. i.e. Use an Event whose state is checked as a condition to exit the thread. (better yet use two events.. one to signal work to be done, the other to exit. wait (WaitForMultipleObjects) on both and then react appropriately.. that way your work thread won't waste cycles running through loops pointlessly.) Second, yes. Even access to resources should be protected (generally) since you want to ensure that the resource is not being modified while you are reading it. (or whatever) That said, some types of resources (small register-sized variables generally) can be read/written without the need of a protecting synchronization object. Check out the Interlocked_______ functions in win32.
-
First, if you're going to use a synhcronization object to protect a flag, use the synchronization object as the flag. i.e. Use an Event whose state is checked as a condition to exit the thread. (better yet use two events.. one to signal work to be done, the other to exit. wait (WaitForMultipleObjects) on both and then react appropriately.. that way your work thread won't waste cycles running through loops pointlessly.) Second, yes. Even access to resources should be protected (generally) since you want to ensure that the resource is not being modified while you are reading it. (or whatever) That said, some types of resources (small register-sized variables generally) can be read/written without the need of a protecting synchronization object. Check out the Interlocked_______ functions in win32.
-
Here's a code sample:
// these handles are created and passed out to whoever'll
// let the thread know when to work and when to quitHANDLE hWorkToBeDone=CreateEvent(0,FALSE,FALSE,0); // resets on wait
HANDLE hTimeToQuit=CreateEvent(0,TRUE,FALSE,0); // once signalled, stays so// Basic thread loop
HANDLE Waitables[]={hTimeToQuit,hWorkToBeDone};
for (DWORD W=~WAIT_OBJECT_0;W!=WAIT_OBJECT_0;)
{
switch (W=WaitForMultipleObjects(2,Waitables,FALSE,INFINITE))
{
case WAIT_FAILED:
// use GetLastError and handle this
break;
case WAIT_OBJECT_0: // It is time to quit
break;
case WAIT_OBJECT_0+1: // There is work to be done
; // Do some work
break;
case WAIT_TIMEOUT: // impossible? we specified no timeout
default: // there should be no other cases. (e.g. for abandoned mutexes)
ASSERT(FALSE);
}
}