使用条件变量优于互斥锁的优点 [英] Advantages of using condition variables over mutex

查看:32
本文介绍了使用条件变量优于互斥锁的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 pthread 中使用条件变量而不是互斥锁有什么性能优势.

I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads.

我发现:没有条件变量,程序员需要让线程不断轮询(可能在关键部分),以检查是否满足条件.这可能非常消耗资源,因为线程会连续忙于这项活动.条件变量是一种无需轮询即可实现相同目标的方法.(https://computing.llnl.gov/tutorials/pthreads)

What I found is : "Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling." (https://computing.llnl.gov/tutorials/pthreads)

但似乎互斥锁调用也是阻塞的(与自旋锁不同).因此,如果一个线程 (T1) 因为其他线程 (T2) 拥有锁而未能获得锁,则 T1 被 OS 置于睡眠状态,并且只有在 T2 释放锁并且 OS 将锁交给 T1 时才会唤醒.线程 T1 并没有真正轮询以获得锁.从这个描述来看,使用条件变量似乎没有性能优势.在任何一种情况下,都不涉及轮询.无论如何,操作系统提供了条件变量范式可以提供的好处.

But it also seems that mutex calls are blocking (unlike spin-locks). Hence if a thread (T1) fails to get a lock because some other thread (T2) has the lock, T1 is put to sleep by the OS, and is woken up only when T2 releases the lock and the OS gives T1 the lock. The thread T1 does not really poll to get the lock. From this description, it seems that there is no performance benefit of using condition variables. In either case, there is no polling involved. The OS anyway provides the benefit that the condition-variable paradigm can provide.

你能解释一下实际发生的事情吗?

Can you please explain what actually happens.

推荐答案

条件变量允许在线程感兴趣的事情发生时向线程发出信号.

A condition variable allows a thread to be signaled when something of interest to that thread occurs.

互斥锁本身不会这样做.

By itself, a mutex doesn't do this.

如果您只需要互斥,那么条件变量不会为您做任何事情.但是,如果您需要知道某事何时发生,则条件变量会有所帮助.

If you just need mutual exclusion, then condition variables don't do anything for you. However, if you need to know when something happens, then condition variables can help.

例如,如果您有一个要处理的项目队列,那么您将拥有一个互斥锁来确保队列的内部结构在被各种生产者和消费者线程访问时是一致的.然而,当队列为空时,消费者线程如何知道什么时候有东西在那里工作呢?如果没有条件变量之类的东西,它将需要轮询队列,在每次轮询时获取和释放互斥锁(否则生产者线程永远无法将某些内容放入队列中).

For example, if you have a queue of items to work on, you'll have a mutex to ensure the queue's internals are consistent when accessed by the various producer and consumer threads. However, when the queue is empty, how will a consumer thread know when something is in there for it to work on? Without something like a condition variable it would need to poll the queue, taking and releasing the mutex on each poll (otherwise a producer thread could never put something on the queue).

使用条件变量可以让消费者发现,当队列为空时,它可以等待条件变量,表明队列已经放入了一些东西.没有轮询 - 该线程什么都不做,直到生产者将某些东西放入队列中,然后发出队列有新项目的信号.

Using a condition variable lets the consumer find that when the queue is empty it can just wait on the condition variable indicating that the queue has had something put into it. No polling - that thread does nothing until a producer puts something in the queue, then signals the condition that the queue has a new item.

这篇关于使用条件变量优于互斥锁的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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