Mutexes vs Monitors - A Comparison

╄→гoц情女王★ 提交于 2019-12-22 15:04:33

问题


From what I have learned about Mutexes - they generally provide a locking capability on a shared resources. So if a new thread wants to access this locked shared resource - it either quits or has to continually poll the lock (and wastes processor cycles in waiting for the lock).

However, a monitor has condition variables which provides a more asynchronous way for waiting threads - by putting them on wait queue and thereby not making them consume processor cycles.

Would this be the only advantage of monitors over mutexes (or any general locking mechanism without condition variables) ?


回答1:


Mutexes are low level construct. They just provide mutual exclusion and memory visibility/ordering. Monitors, on the other hand, are higher level - they allow threads to wait for an application specific condition to hold.

So, in some cases monitors are just overkill over a simple lock/unlock, but in most cases mutexes alone are not nearly enough - so you see them used with one or more condition variables - conceptually using monitors equivalent.




回答2:


I think, a monitor locks an object (Multi thread cannot access the object at the same time.) While a mutex locks a process (multi-thread only one can go through the process.)



来源:https://stackoverflow.com/questions/7669541/mutexes-vs-monitors-a-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!