0

我有多个消费者线程和一个生产者线程。生产者线程将数据写入属于某个消费者线程的映射,并向消费者线程发送信号。当我插入和删除数据时,我在地图周围使用互斥锁。但是,这种方法在速度性能方面看起来并不高效。您能否建议另一种方法而不是需要互斥锁和解锁的映射,我认为互斥会减慢传输速度。

4

2 回答 2

2

however this approach looks not efficient in terms of speed performance. Can you suggest another approach instead of map which requires mutex locks and unlocks and I think mutex slows down the transmission.

You should use a profiler to identify where the bottleneck is.


Producer thread writes the data into a map belong to a certain consumer thread and sends a signal to the consumer thread.

The producer should not be concerned what kind of data structure the consumer uses - it is a consumer's implementation detail. Keep in mind that inserting a value into a map requires a memory allocation (unless you are using a custom allocator) and memory allocation internally takes locks as well to protect the state of the heap. The end result is that locking a mutex around map::insert operation may lock it for too long actually.

A simpler and more efficient design would be to have an atomic queue between the producer and consumer (e.g. pipe, TBB concurrent_bounded_queue which pre-allocates its storage so that push/pop operations are really quick). Since your producer communicates directly to each consumer that queue is one-writer-one-reader and it can be implemented as a wait-free queue (or ring buffer a-la C++ disruptor).

于 2013-09-27T14:09:05.883 回答
0

Andrei Alexandrescu 提出了一个很好的观点,即您应该测量您的代码(https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920),这与我的建议相同给你,这是衡量你的代码,看看你在基线测试和运行单线程的测试之间得到了什么性能差异:

  1. 使用单线程插入数据以映射到上面列出的数据所需的时间
  2. 使用单线程插入数据以映射到上面列出的数据并使用互斥锁所需的时间

如果您仍在寻找线程安全容器,您可能需要查看英特尔在http://www.threadingbuildingblocks.org/docs/help/reference/containers_overview/concurrent_queue_cls.htm上的线程安全容器开源实现.

此外,作为消费者线程实现的建议,您可能需要阅读Herb Sutter在其网站上发布的ActiveObject文章:http: //herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active -objects-instead-of-naked-threads/

如果你能提供更多细节,比如为什么必须一直锁定地图,我们或许可以起草一个性能更好的机制。

于 2013-09-27T14:21:07.673 回答