How to test mutex?

How to test mutex?

Post by Arriv » Thu, 17 Feb 2005 12:58:35

Hi, all,

My program runs normally even without the use of mutex. Now I have added
the mutexes, how can I test them? Or ask in another way, how to
generate/find bugs in concurrency programs?

The program is an simple HTTP server with multithread support for our
course project. So far I have only protected all the global variables
and the file IOs by mutex. Are there any places else I need to pay
attention to?

Or any good resource on thread safe? I have tried google but didn't get
a good answer.

Many thanks.

Life is the chance to do something.

How to test mutex?

Post by Mile Blent » Thu, 17 Feb 2005 22:50:30

You can try inserting calls to delay execution (sleep) before
releasing mutexes and in this way generate situations where two
or more threads try to lock the same mutex to see if there are
any deadlocks. This is simple if you used your own wrapper around
mutex lock/unlock.

Make sure you don't call some function from within the locked mutex that
also tries to lock that mutex. That's a sure deadlock :)

For access to global variables that needs to atomic it's a good practice
to access these vars through get/set wrapper funcs and lock/unlock
of the mutex inside them or when using OO language to access the same vars
through a container object and it's wrapper funcs so the mutex handling is
located in one place. ~eclewis/papers/cse00.pdf