如何实现无锁的,但是阻塞行为?

编程入门 行业动态 更新时间:2024-10-22 15:32:08
本文介绍了如何实现无锁的,但是阻塞行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我执行的密集网络应用无锁单生产者单个消费者队列。我有一大堆的接收自己单独的队列工作,然后他们出列和工作线程的过程。

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.

更多推荐

如何实现无锁的,但是阻塞行为?

本文发布于:2023-11-30 20:03:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!