Reputation: 2907
Let's say I call
h=CreateMutex(NULL,FALSE,"full");
y=WaitForSingleObject(h,INFINITE);
//Read from a queue (critical section)
ReleaseMutex(h);
What issues can arise that can lead to an access violation reading a location?
For example is it possible for multiple threads to enter that critical section as the same time?
Upvotes: 0
Views: 411
Reputation: 163317
Although you're storing the results of those functions in variables, you're not reading them to determine whether the functions succeeded. Perhaps you didn't create or open the given mutex, so h
is 0. Or perhaps instead of acquiring ownership of the mutex, the the wait failed. In either case, you should call GetLastError
to find out why, and then don't execute the protected section of code.
It's possible for a mutex to be abandoned. That means that the thread that previously owned the mutex was terminated before it released ownership of the mutex. (Only a mutex can be abandoned; critical sections and semaphores don't have thread affinity the way mutex objects do.) If that happens, you'll still be granted ownership of the mutex, but you can't really trust the validity of the data that the mutex is supposed to be protecting because the previous owner might not have left things in a stable state before it terminated.
If you call the functions correctly and check for errors, there's no way for multiple threads to enter a critical section simultaneously. That's the whole purpose of synchronization objects.
Upvotes: 1