进程在其生命周期内有 1 个主线程和 1 到 50 个其他工作线程。 当主线程接受新连接时,它会将其保存在 boost::unordered_map 中,我们称它为“new con container”。工作线程时不时地检查“new con container”中的新连接。出于同步目的,有一个 boost::mutex。当主线程写入“new con container”时,它会锁定这个互斥体。工作线程在检查此容器时也会锁定互斥体。
有没有办法让工作线程不锁定互斥量并从“新的 con 容器”中安全地读取线程?工作线程在套接字上执行实时操作,因此锁定互斥量会降低性能。
最佳答案
如果您有一个作者和多个读者,请考虑使用 shared_mutex :
The class boost::shared_mutex provides an implementation of a multiple-reader / single-writer mutex.
关于c++ - 如何在多个线程之间进行同步。只有一个在写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11324715/