本文介绍了互斥与监控-比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对Mutexes的了解,它们通常在共享资源上提供锁定功能.因此,如果一个新线程想要访问此锁定的共享资源-它要么退出要么必须连续轮询该锁(并且在等待锁时浪费了处理器周期).

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) ?

推荐答案

互斥体是低级结构.它们只是提供互斥和内存可见性/顺序.另一方面,监视器是较高级别的-它们允许线程等待特定于应用程序的条件成立.

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.

这篇关于互斥与监控-比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 04:20