mutable boost :: mutex是否可以分离锁定和等待函数?

时间:2011-08-25 23:53:09

标签: c++ multithreading boost mutex mutable

所以我有像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()
}

那怎么办呢?

1 个答案:

答案 0 :(得分:4)

您可以使用

boost::shared_mutex  m

Reader()
 shared_lock   lock(m)

Writer()
 upgradeable_lock lck(m)
 upgrade_to_unique_lock uniqueLock(lck);

了解有关助推锁的更多信息:Boost thread sync mechanisms

要了解您正在处理的问题的类别:Wikpedia Link to Reader-WriterLock

要了解有关POSIX读写器锁定的更多信息,它可以直接为您提供读写锁定,语法简单明了:POSIX reader-witer locks