What is mutex?

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.

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.

What is mutex

解释的真好wwwww


当我在工作中需要进行一场热烈讨论的时候,我会用到我桌上的一只橡皮鸡。它专门用来应对这种场合,只有拿着橡皮鸡的人才被允许说话。如果橡皮鸡不在你手上,那你就不能说话。你只能表示你想要橡皮鸡,然后等着直到你拿到鸡以后再说话。一旦你结束发言,你就可以把橡皮鸡还给中间人,他会把橡皮鸡交给下一个想要发言的人。这种机制保证了大家的发言不会彼此覆盖,并且能够拥有各自发言的空间。

在上面的例子中把橡皮鸡换成mutex,把发言人换成线程,你就已经得到了mutex的概念了。

当然,不存在什么橡皮mutex,只有橡皮鸡。我的猫咪们曾经有过一只橡皮老鼠,但是被他们吃了。

在你使用橡皮鸡之前,你需要问问你自己是不是真的需要让5个人同时在一个房间里,是不是让一个人单独在屋子里把所有的活都干了比较简单。

猜你喜欢

转载自blog.csdn.net/lokira518/article/details/53005432
今日推荐