我执行的密集网络应用无锁单生产者单个消费者队列。我有一大堆的接收自己单独的队列工作,然后他们出列和工作线程的过程。
I'm implementing a lock-free single producer single consumer queue for an intensive network application. I have a bunch of worker threads receiving work in their own separate queues, which they then dequeue and process.
从这些队列中删除的锁有很大的改善高负载下的性能,但他们不再阻止当队列为空,从而导致CPU使用率猛增。
Removing the locks from these queues have greatly improved the performance under high load, but they no longer block when the queues are empty, which in turn causes the CPU usage to skyrocket.
如何能有效地导致线程阻塞,直到它能够成功出队的东西或者被杀害/中断?
How can I efficiently cause a thread to block until it can successfully dequeue something or is killed/interrupted?
推荐答案如果你是在Linux上,考虑使用 futex的。它通过使用原子操作,而不是内核调用像一个互斥体将提供非锁定实现的性能,但是你应该需要设置的过程中闲置因为一些条件不是真(即锁争用),它会然后进行相应的内核调用把进程睡眠和唤醒它备份在今后的活动。它基本上是一个非常快速的信号。
If you're on Linux, look into using a Futex. It provides the performance of a non-locking implementation by using atomic operations rather than kernel calls like a mutex would, but should you need to set the process to idle because of some condition not being true (i.e., lock-contention), it will then make the appropriate kernel calls to put the process to sleep and wake it back up at a future event. It's basically like a very fast semaphore.
更多推荐
如何实现无锁的,但是阻塞行为?
发布评论