Skip to content
Advertisement

Mutex Locks vs Peterson’s Algorithm?

Do mutex locks ensure bounded waiting condition ? Is it possible if two threads are trying to get hold of a lock, but only one process (just by luck) gets it again and again. Since Peterson’s Algorithm ensures bounded waiting, is it better to use that instead of mutex locks ?

Advertisement

Answer

It is possible to have unbounded wait with mutices, if for instance locking attempts keep coming in on a mutex, at least in C++ std::mutex there’s no guaranteed first comes first gets.

However this shouldn’t really be a concern – Unless you have some lock with many many threads locking all the time (and even in that case it’s very unlikely to cause some starvation situation).

The best thing to do is always use standard library locking mechanism and not write your own mutices.

Advertisement