Community
cancel
Showing results for 
Search instead for 
Did you mean: 
jim_semeda
Beginner
49 Views

How to find who is waiting on a mutex

I am implementing a message queue that can be accessed by several threads to post and retrieve messages.
I am using a mutex to synchronize the queue's access by threads. I need to find out who is waiting for the lock to retrieve messages when a message arrives. Anyone knows how to find out who is waiting on a mutex lock?
0 Kudos
3 Replies
jimdempseyatthecove
Black Belt
49 Views

The handle on a Mutex may be the address of an opaque structure. In the debugger you can view the memory, as DWORD or QWORD, at the address indicated by the Mutex handle. Observe any changes across WaitForSinglObject that obtains the lock. Also, observe any changes across a ReleaseMutex. By this observation you might be able to descern some useful information. i.e. is the thread ID of the owner of the lock stored in the opaque structure? It might not be. The waiting threads may be stored in a list, whos list head is in the opaque structure.

Using values derived from an opaque structure is generaly not advised in a release version of an application (the contents of the opaque structure are subject to change with new versions of the OS).

A better route is for you to write a wrapper function to lock and release theMutex. This wrapper function can then insert and remove an identifier of your design.

Depending on your design requirements a hybrid approach might be advised. e.g. the queuing threads don't suspend on insert of message, but the retrieving thread blocks onlyon empty.

Jim Dempsey

jim_semeda
Beginner
49 Views

Thanks for the reply ... however I wasn't looking for a debugging method, but rather a way to do it in the program. I wanted to find out who is waiting on a lock before I post a message so I can just deliver it to whomever is waiting rather than posting it, and if no one was waiting I would just post it in that case.
jimdempseyatthecove
Black Belt
49 Views

From my prior message

>>

A better route is for you to write a wrapper function to lock and release theMutex. This wrapper function can then insert and remove an identifier of your design.

<<

Since you "rather a way to do it in the program" you therefor can program the Mutex lock and release functions. Make a function that you call to perform the lock and release but additionaly save the thread identification information.

If you are not in control of all the programs the perform the locks then you would be unable to insert the identifier information. On the other hand if everyone is running different instances of the same program then you only need to add the identifyer tracking in one application.

Jim

Reply