Take the 2-minute tour ×

I'm trying to use WaitForSingleObject(fork[leftFork], Infinite); to lock a variable using multiple threads but it doesn't seem to lock anything

I set the Handle fork[5] and then use the code below but it doesn't seem to lock anything.

while(forks[rightFork] == 0 || forks[leftFork] == 0) Sleep(0);
WaitForSingleObject(fork[leftFork], INFINITE);

WaitForSingleObject(fork[rightFork], INFINITE);

I have tried as a WaitForMultipleObjects as well and same result. When I create the mutex I use fork[i]= CreateMutex(NULL, FALSE,NULL);

I was wondering if this is only good for each thread or do they share it?

share|improve this question
Btw, you may want to tag Windows-specific things as such. Also, to format code, select it and press CTRL+K. –  EboMike Feb 21 '11 at 22:11
First off, check the return value from WaitForSingleObject() agains WAIT_FAILED. If they are equal, call GetLastError() and proceed from there. –  André Caron Feb 21 '11 at 22:15
How do you know it's not locking anything? What's expected/actual behavior? –  Erik Feb 21 '11 at 22:22
I reformatted your code, and your text. It pays to take care of the details. C++ is case-sensitive, but your letter case was all over the place. Threading is hard, the details really matter. I didn't go as far as renaming the fork array to spoons, but it was tempting!! –  David Heffernan Feb 21 '11 at 22:45
You appear to be filling the fork array with mutex handles, but then follow this up by decrementing the handles. This is incorrect - all you can do with a mutex handle is wait for it or release it. –  Jon Feb 22 '11 at 17:55

1 Answer 1

First of all, you haven't shown enough code for us to be able to help you with any great certainty of correctness. But, having made that proviso, I'm going to try anyway!

Your use of the word fork suggests to me that you are approaching Windows threading from a pthreads background. Windows threads are a little different. Rather confusingly, the most effective in-process mutex object in Windows is not the mutex, it is in fact the critical section.



Having said that, it would appear that all you are trying to do is to synchronize the decrementing of an array of integer values. If that is so then you can do this most simply in a lock free manner with InterlockIncrement or one of its friends.

You only need to use a mutex when you are performing cross process synchronization. Indeed you should only use a mutex when you are synchronizing across a process because critical sections perform so much better (i.e. faster). Since you are updating a simple array here, and since there is no obviously visible IPC going on, I can only conclude that this really is in-process.

If I'm wrong and you really are doing cross-process work and require a mutex then we would need to see more code. For example I don't see any calls to ReleaseMutex. I don't know exactly how you are creating your mutexes.

If this doesn't help, please edit your question to include more code, and also a high level overview of what you are trying to achieve.

share|improve this answer
Sorry about that, im not sure how to attach a file but my code is below. This is for a class im in and with working full time and the teacher only availible during the day and he doesnt respond to emails, ive been limited to resources, but we are only allowed to use locks in our solution. Ive tried setting up the lock for single object and multiple objects with no luck. Im trying to lock each part of the array at different times and have only been successful by locking out the whole array at once. –  tim Feb 21 '11 at 22:52
@tim OK, a critical section is a lock. You can't attach a file, but you can include code in a question. Really, why don't you use critical sections? InterlockedIncrement would be much better, but if you are being taught about locks, then locks it is. But start with critical sections!! –  David Heffernan Feb 21 '11 at 22:54
Would i use the locks to create the critical section First i Set the handle HANDLE fork[5]; for(int i = 0; i < 5; i++) fork[i]=CreateEvent(NULL, FALSE, FALSE, NULL); Then edit the varible and try to lock it before i do so while(forks[rightFork] == 0 || forks[leftFork] == 0 || (philoState[left] == HUNGRY && eatCount[me] > eatCount[left])) Sleep(0); { WaitForMultipleObjects(leftFork, fork, FALSE, INFINITE); // WaitForSingleObject(fork[leftFork], INFINITE); forks[leftFork]--; WaitForMultipleObjects(rightFork, fork, FALSE, INFINITE); forks[rightFork]--; –  tim Feb 21 '11 at 23:00
@tim No, the critical section object is the lock. It is the canonical in-process lock in Windows. Read the Stack Overflow answer I linked to and the pages that answer refers to. I think the confusion is that lock and mutex are synonyms, but the Windows Mutex is not the best kind of lock for the task you have been set. –  David Heffernan Feb 21 '11 at 23:02
@tim You haven't had time to read all these links and digest them. Please try to do that. –  David Heffernan Feb 21 '11 at 23:03

Your Answer


