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

…衆ロ難τιáo~ 提交于 2019-12-11 08:37:24

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7198561/mutable-boostmutex-is-it-possible-to-separate-lock-and-wait-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!