本文介绍了信号量和互斥量在实现上有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到互斥量和二进制信号量仅在一个方面有所不同,就互斥量而言,锁定线程必须解锁,但是在信号量中,锁定和解锁线程可以不同吗?

I read that mutex and binary semaphore are different in only one aspect, in the case of mutex the locking thread has to unlock, but in semaphore the locking and unlocking thread can be different?

哪个效率更高?

推荐答案

假设您知道sempahore和互斥锁之间的基本区别:

Assuming you know the basic differences between a sempahore and mutex :

为实现快速,简单的同步,请使用关键部分.

要跨进程边界同步线程,请使用互斥锁.

要同步访问受限资源,请使用信号灯.

除了互斥对象具有所有者这一事实之外,还可以针对不同的用法优化这两个对象.互斥对象只能保留很短的时间.违反此规定可能会导致性能下降和调度不公.例如,即使另一个线程已被阻塞,正在运行的线程也可能被允许获取互斥锁,从而创建了死锁.信号量可以提供更多的公平性,或者可以使用多个条件变量来强制公平性.

Apart from the fact that mutexes have an owner, the two objects may be optimized for different usage. Mutexes are designed to be held only for a short time; violating this can cause poor performance and unfair scheduling. For example, a running thread may be permitted to acquire a mutex, even though another thread is already blocked on it, creating a deadlock. Semaphores may provide more fairness, or fairness can be forced using several condition variables.

这篇关于信号量和互斥量在实现上有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:51