multithreading 互斥锁、信号量和自旋锁之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23511058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:25:35  来源:igfitidea点击:

Difference between Mutex, Semaphore & Spin Locks

multithreadingipcmutexsemaphorespinlock

提问by Novice

I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and Sleeping) Mechanism, and Spin Locks are Synchronous but Non-sleeping Mechanism.

我正在用 IPC 做实验,尤其是 Mutex、Semaphore 和 Spin Lock。我学到的是 Mutex 用于异步锁定(带睡眠(根据我在 NET 上读到的理论))机制,信号量是同步锁定(带信号和睡眠)机制,而自旋锁是同步但非睡眠机制。

Can anyone help me to clarify these stuff deeply? And another doubt is about Mutex, when I wrote program with thread & mutex, while one thread is running another thread is not in Sleep state but it continuously tries to acquire the Lock. So Mutex is sleeping or Non-sleeping???

谁能帮我深入澄清这些东西?另一个疑问是关于互斥锁,当我用线程和互斥锁编写程序时,当一个线程正在运行时,另一个线程不处于睡眠状态,但它不断尝试获取锁。所以互斥锁是睡觉还是不睡觉???

回答by fante

First, remember the goal of these 'synchronizing objects':

首先,记住这些“同步对象”的目标:

These objects were designed to provide an efficientand coherentuse of 'shared data'between more than 1 threadamong 1 process or from different processes.

这些目的旨在提供一种有效的一致的使用的“共享数据”之间超过1个线程间1个过程或从不同的过程。

These objects can be 'acquired'or 'released'.

这些对象可以是'acquired''released'

That is it!!! End of story!!!

这就对了!!!故事结局!!!

Now, if it helps to you, let me put my grain of sand:

现在,如果它对你有帮助,让我放上我的一粒沙子:

1) Critical Section= User object used for allowing the execution of just one active threadfrom many others within one process. The other non selected threads (@ acquiring this object) are put to sleep.

1) 临界区= 用户对象,用于允许一个进程内的许多其他线程仅执行一个活动线程。其他未选择的线程(@获取此对象)将进入sleep

[No interprocess capability, very primitive object].

[没有进程间能力,非常原始的对象]。

2) Mutex Semaphore (aka Mutex)= Kernel object used for allowing the execution of just one active threadfrom many others, within one processor among different processes. The other non selected threads (@ acquiring this object) are put to sleep. This object supports thread ownership, thread termination notification, recursion (multiple 'acquire' calls from same thread) and 'priority inversion avoidance'.

2) Mutex Semaphore (aka Mutex)= 内核对象,用于允许在一个进程内不同进程之间仅执行来自许多其他线程的一个活动线程。其他未选择的线程(@获取此对象)将进入sleep。该对象支持线程所有权、线程终止通知、递归(来自同一线程的多个“获取”调用)和“优先级反转避免”。

[Interprocess capability, very safe to use, a kind of 'high level' synchronization object].

[进程间能力,使用非常安全,一种'高级'同步对象]。

3) Counting Semaphore (aka Semaphore)= Kernel object used for allowing the execution of a group of active threadsfrom many others, within one processor among different processes. The other non selected threads (@ acquiring this object) are put to sleep.

3) Counting Semaphore (aka Semaphore)= 用于允许在一个进程内在不同进程之间执行来自许多其他线程一组活动线程的内核对象。其他未选择的线程(@获取此对象)将进入sleep

[Interprocess capability however not very safe to use because it lacks following 'mutex' attributes: thread termination notification, recursion?, 'priority inversion avoidance'?, etc].

[但是使用进程间能力不是很安全,因为它缺少以下“互斥”属性:线程终止通知、递归?、“优先级反转避免”?等]。

4) And now, talking about 'spinlocks', first some definitions:

4)现在,谈论“自旋锁”,首先是一些定义:

Critical Region= A region of memory shared by 2 or more processes.

临界区 = 由 2 个或多个进程共享的内存区域。

Lock= A variable whose value allows or denies the entrance to a 'critical region'. (It could be implemented as a simple 'boolean flag').

Lock= 一个变量,其值允许或拒绝进入“临界区”。(它可以实现为一个简单的“布尔标志”)。

Busy waiting= Continuosly testing of a variable until some value appears.

忙等待 = 不断测试变量直到出现某个值。

Finally:

最后:

Spin-lock (aka Spinlock)= A lockwhich uses busy waiting. (The acquiring of the lockis made by xchgor similar atomic operations).

自旋锁(又名自旋锁)=使用忙等待的。(的获取是通过xchg或类似的原子操作完成的)。

[No thread sleeping, mostly used at kernel level only. Ineffcient for User level code].

[没有线程休眠,主要仅用于内核级别。对用户级代码效率低下]。

As a last comment, I am not sure but I can bet you some big bucks that the above first 3 synchronizing objects (#1, #2 and #3) make use of this simple beast (#4) as part of their implementation.

作为最后一条评论,我不确定,但我可以向您打赌,上面的前 3 个同步对象(#1、#2 和 #3)利用这个简单的野兽(#4)作为其实现的一部分。

Have a good day!.

祝你有美好的一天!。

References:

参考:

-Real-Time Concepts for Embedded Systems by Qing Li with Caroline Yao (CMP Books).

-Qing Li 和 Caroline Yao 的嵌入式系统实时概念(CMP 书籍)。

-Modern Operating Systems (3rd) by Andrew Tanenbaum (Pearson Education International).

- Andrew Tanenbaum(培生教育国际)的现代操作系统(第三名)。

-Programming Applications for Microsoft Windows (4th) by Jeffrey Richter (Microsoft Programming Series).

-Jeffrey Richter 的 Microsoft Windows 编程应用程序(第 4 期)(Microsoft 编程系列)。

回答by smyrgl

Here is a great explanation of the difference between semaphores and mutexes:

这是对信号量和互斥体之间区别的很好的解释:

http://blog.feabhas.com/2009/09/mutex-vs-semaphores-–-part-1-semaphores/

http://blog.feabhas.com/2009/09/mutex-vs-semaphores-–-part-1-semaphores/

The short answer has to do with ownership at least with binary semaphores but I suggest you read the entire article.

简短的回答至少与二进制信号量的所有权有关,但我建议您阅读整篇文章。