mutable boost::mutex 是否可以将锁定和等待功能分开?

发布于 2024-12-01 06:20:08 字数 480 浏览 3 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-12-08 06:20:08

您可以使用

boost::shared_mutex  m

Reader()
 shared_lock   lock(m)

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文