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

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

问题描述

我想知道使用条件变量而不是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并没有真正轮询以获取锁.根据此描述,使用条件变量似乎没有性能上的好处.无论哪种情况,都不会涉及轮询.无论如何,OS提供了条件变量范例可以提供的好处.

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天全站免登陆