开发者

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

开发者 https://www.devze.com 2023-03-31 09:19 出处:网络
So I have functions like read that can be called at the same time from multiple threads. butalso I have a function to write that needs to lock all that read functions. Where to get example of creating

So I have functions like read that can be called at the same time from multiple threads. but also I have a function to write that needs to lock all that read functions. Where to get example of creating such archetecture?

I get that we can have:

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

and:

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()
}
开发者_如何转开发

So how to do such thing?


You can use

boost::shared_mutex  m

Reader()
 shared_lock   lock(m)

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

To know more about boost-locks : Boost thread sync mechanisms

To know about the class of the problem you are dealing with : Wikpedia Link to Reader-WriterLock

To know more about POSIX reader-writer lock, which directly gives you reader write lock with much simple syntax : POSIX reader-witer locks

0

精彩评论

暂无评论...
验证码 换一张
取 消