可变boost::mutex可以分离锁和等待函数

mutable boost::mutex is it possible to separate lock and wait functions?

本文关键字:等待 分离 函数 boost mutex 可变      更新时间:2023-10-16

所以我有像read这样的函数,可以从多个线程同时调用。但我也有一个write的函数需要锁定所有read的函数。在哪里可以找到创建这种架构的例子?

我知道我们可以有:

mutable boost::mutex the_read_mutex;
mutable boost::mutex the_write_mutex;

:

void write()
{
    // make all new readers wait and wait for all other currently running read threads();
}
void read()
{
    // do not make all new readers wait, and wait for all currently running write thread()
}

那么怎么做呢?

可以使用

boost::shared_mutex  m
Reader()
 shared_lock   lock(m)
Writer()
 upgradeable_lock lck(m)
 upgrade_to_unique_lock uniqueLock(lck);

了解更多关于Boost锁:Boost线程同步机制

要了解您正在处理的问题的类别:维基百科链接到Reader-WriterLock

了解更多关于POSIX读写锁的信息,它以非常简单的语法直接为您提供了读写锁: