什么是互斥锁? [英] What is a mutex?

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

问题描述

互斥锁是一种经常用于解决多线程问题的编程概念.我对社区的问题:

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community:

什么是互斥量以及如何使用它?

What is a mutex and how do you use it?

推荐答案

当我在工作中进行激烈的讨论时,我会使用放在办公桌上的橡皮鸡.拿着鸡的人是唯一被允许说话的人.如果你不拿着鸡,你就不能说话.你只能表明你想要鸡肉,等到你得到它再说话.一旦你说完,你可以把鸡交还给主持人,主持人将把它交给下一个发言的人.这样可以确保人们不会互相争吵,也有自己的谈话空间.

When I am having a big heated discussion at work, I use a rubber chicken which I keep in my desk for just such occasions. The person holding the chicken is the only person who is allowed to talk. If you don't hold the chicken you cannot speak. You can only indicate that you want the chicken and wait until you get it before you speak. Once you have finished speaking, you can hand the chicken back to the moderator who will hand it to the next person to speak. This ensures that people do not speak over each other, and also have their own space to talk.

用互斥体代替鸡,用线程代替人,你基本上有了互斥体的概念.

Replace Chicken with Mutex and person with thread and you basically have the concept of a mutex.

当然,没有橡胶互斥锁这样的东西.只有橡皮鸡.我的猫曾经有一只橡皮老鼠,但他们吃了它.

Of course, there is no such thing as a rubber mutex. Only rubber chicken. My cats once had a rubber mouse, but they ate it.

当然,在你使用橡皮鸡之前,你需要问问自己,你是否真的需要一个房间里的5个人,一个人在一个房间里自己做所有的工作会不会更容易.实际上,这只是类比的延伸,但你明白了.

Of course, before you use the rubber chicken, you need to ask yourself whether you actually need 5 people in one room and would it not just be easier with one person in the room on their own doing all the work. Actually, this is just extending the analogy, but you get the idea.

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

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