Mutex WaitForSingleObject does not Signal
-
I piece of code that is executed by 4 threads. I want to Serialize it one at a time So I Create a Mutex handle = CreateMutex(NULL,FALSE,NULL); I observe that the HANDLE its not NULL Then I "dwWaitResult = WaitForSingleObject(sysblk.single_thread,INFINITE)" (I am running this under VS debugger and have a few breakpoints before I do the ReleaseMutex) Then finally the first thread gets to the end of the serialized code. I do a "ReleaseMutex(sysblk.single_thread)" it returns 1 however When I look back at the VS Threads Window and click on each the 3 threads there is an inverted arrow at the WaitForSingleObject indicating that none of the threads were signaled Thanks
-
What is
sysblk.single_thread
in the above? You should be waiting on and releasing the Mutex handle, as described at Using Mutex Objects (Windows)[^].Sorry that was a typo as I typed the code and didn't cut paste it was meant to be WaitForSingleObject(handle,INFINITE); and ReleaseMutex(handle); Let me ask question after the first thread goes thru the WaiForSingleObject it turns from signaled to non-signaled then if more then one thread is waitting when the ReleaseMutex is executed it lets only ONE thread at a time to process while the other remain waiting till the next ReleaseMutex correct ?
-
Sorry that was a typo as I typed the code and didn't cut paste it was meant to be WaitForSingleObject(handle,INFINITE); and ReleaseMutex(handle); Let me ask question after the first thread goes thru the WaiForSingleObject it turns from signaled to non-signaled then if more then one thread is waitting when the ReleaseMutex is executed it lets only ONE thread at a time to process while the other remain waiting till the next ReleaseMutex correct ?
That should be the way is works. Each thread should acquire the Mutex (i.e. lock it) in order to protect the data. When they finish the critical section they should Release it. The point being that you only want one thread through this path at any one time.
-
I piece of code that is executed by 4 threads. I want to Serialize it one at a time So I Create a Mutex handle = CreateMutex(NULL,FALSE,NULL); I observe that the HANDLE its not NULL Then I "dwWaitResult = WaitForSingleObject(sysblk.single_thread,INFINITE)" (I am running this under VS debugger and have a few breakpoints before I do the ReleaseMutex) Then finally the first thread gets to the end of the serialized code. I do a "ReleaseMutex(sysblk.single_thread)" it returns 1 however When I look back at the VS Threads Window and click on each the 3 threads there is an inverted arrow at the WaitForSingleObject indicating that none of the threads were signaled Thanks
Take a look at Using Mutex Objects (Windows)[^] The mutex isn't acquired by creating it, it is acquired by successfully waiting on it.
-
Take a look at Using Mutex Objects (Windows)[^] The mutex isn't acquired by creating it, it is acquired by successfully waiting on it.
In the Microsoft example there are 2 threads so .. 1) thread gains ownership of the signaled object and the second waits In may case they are 4 so If thread 1 gains ownership do threads 2 - 4 waits if so When the ReleaseMutex are all released or only one at a time if so Which one
-
That should be the way is works. Each thread should acquire the Mutex (i.e. lock it) in order to protect the data. When they finish the critical section they should Release it. The point being that you only want one thread through this path at any one time.
-
Sorry for the delayed response but I have been very busy at work But can Multiple threads wait on the Mutex with the WaitForSingleObject And then which is released with the ReleaseMutex the one waiting the longest
-
In the Microsoft example there are 2 threads so .. 1) thread gains ownership of the signaled object and the second waits In may case they are 4 so If thread 1 gains ownership do threads 2 - 4 waits if so When the ReleaseMutex are all released or only one at a time if so Which one
ForNow wrote:
do threads 2 - 4 waits
Yes.
ForNow wrote:
When the ReleaseMutex are all released or only one at a time
It depends on the scheduler. Thread scheduling is a very interesting topic in OS design, and the rules can vary from platform. But a general rule is that the waiting thread with the highest priority runs. However some OSs throw in a bit of priority inversion on occasion to free up any deadlocks. Within the same priority group you might decide on say, 'last thread run', or 'first thread waiting' to decide which to activate and give the CPU to. A bit like FIFO, LRU LIFO etc, there are a lot of designs. The important point though is that the OS does this for you so no need to worry about it. Use the example from Microsoft and all will be well.
-
ForNow wrote:
do threads 2 - 4 waits
Yes.
ForNow wrote:
When the ReleaseMutex are all released or only one at a time
It depends on the scheduler. Thread scheduling is a very interesting topic in OS design, and the rules can vary from platform. But a general rule is that the waiting thread with the highest priority runs. However some OSs throw in a bit of priority inversion on occasion to free up any deadlocks. Within the same priority group you might decide on say, 'last thread run', or 'first thread waiting' to decide which to activate and give the CPU to. A bit like FIFO, LRU LIFO etc, there are a lot of designs. The important point though is that the OS does this for you so no need to worry about it. Use the example from Microsoft and all will be well.
-
they arent, so you are OK.