views:

175

answers:

2

I have a multithreaded application written in C++. And I'm using mutex for file writes. I have a suspicion that somewhere during the execution of the program, the mutex isn't being released.
So I was wondering if there was a way to check for mutex locks and releases on a file, programmatically or otherwise.
I'm running the code on SuseLinux, btw.
Thanks,

+8  A: 

Welcome to the wonderful world of debugging multi-threaded code. There is no magic bullet to solve your problems, but at the very least you should be using RAII idioms to manage your mutex. This means wrapping the mutex in a C++ class that claims the mutex when instances of the class are created and releases it when it (the class instance) is destroyed. You can also profitably log the claim/releases, but be aware that this may introduce timing bugs and artefacts.

anon
Boost::Threads library has a very nice set of portable RAII mutex... helps a lot with avoiding deadlock from lingering lock.
ceretullis
A: 

On solution is to use a timer around a mutex. For example, one would wait 500ms on a mutex and if the time limit expires, fire off an exception or alert the user. Perhaps you could implement something like this that would write to a debug log.

Thomas Matthews
I would strongly recommend you do not do this. In my experience, MT bugs are better fixed by thinking, rather than hacking. And anything that introduces timing dependencies only makes MT issues more opaque
anon