锁、互斥锁和信号量有什么区别? [英] What is the difference between lock, mutex and semaphore?

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

问题描述

我听说过这些与并发编程有关的词,但是锁、互斥体和信号量之间有什么区别?

I've heard these words related to concurrent programming, but what's the difference between lock, mutex and semaphore?

推荐答案

一个锁只允许一个线程进入被锁定的部分,并且该锁不与任何其他进程共享.

A lock allows only one thread to enter the part that's locked and the lock is not shared with any other processes.

互斥锁与锁相同,但它可以是系统范围的(由多个进程共享).

A mutex is the same as a lock but it can be system wide (shared by multiple processes).

信号量 与互斥锁的作用相同,但允许 x 个线程进入,例如可以使用限制同时运行的cpu、io或ram密集型任务的数量.

A semaphore does the same as a mutex but allows x number of threads to enter, this can be used for example to limit the number of cpu, io or ram intensive tasks running at the same time.

有关互斥锁和信号量之间差异的更详细帖子,请阅读此处.

For a more detailed post about the differences between mutex and semaphore read here.

您还有读/写锁,可以在任何给定时间允许无限数量的读者或 1 个作者.

You also have read/write locks that allows either unlimited number of readers or 1 writer at any given time.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆