mutable boost :: mutex是否可以分离锁和等待函数? [英] mutable boost::mutex is it possible to separate lock and wait functions?

查看:567
本文介绍了mutable boost :: mutex是否可以分离锁和等待函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些功能,如 read ,可以同时从多个线程调用。但也有一个函数,需要锁定所有函数。在哪里得到创造这样archetecture的例子?

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?

我认为我们可以有:

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()
}

那么如何做这样的事情呢?

So how to do such thing?

推荐答案

您可以使用

boost::shared_mutex  m

Reader()
 shared_lock   lock(m)

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

要了解更多关于boost-locks的信息: Boost线程同步机制

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

您处理的问题类别: Wikpedia链接到Reader-WriterLock

要了解更多关于POSIX读写器锁的信息,它直接给读者写锁提供了很简单的语法: POSIX阅读器锁

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

这篇关于mutable boost :: mutex是否可以分离锁和等待函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆