例如升压shared_mutex(多次读取/一次写入)? [英] Example for boost shared_mutex (multiple reads/one write)?

查看:130
本文介绍了例如升压shared_mutex(多次读取/一次写入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了经常读一些数据的多线程应用程序,偶尔的数据将被更新。眼下一个互斥体保持访问这些数据的安全,但它是昂贵的,因为我想多线程可以同时读取,而其中只有锁定需要更新的时候(更新线程可以等待其他线程完成)

I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it's expensive because I would like multiple threads to be able to read simultaneously, and only lock them out when an update is needed (the updating thread could wait for the other threads to finish).

我想这是的boost :: shared_mutex 是应该做的,但我不是关于如何使用它清晰,并没有发现一个明显的例子

I think this is what boost::shared_mutex is supposed to do, but I'm not clear on how to use it, and haven't found a clear example.

有没有人有一个简单的例子,我可以用它来开始?

Does anyone have a simple example I could use to get started?

推荐答案

它看起来像你会做这样的事情:

It looks like you would do something like this:

boost::shared_mutex _access;
void reader()
{
  // get shared access
  boost::shared_lock<boost::shared_mutex> lock(_access);

  // now we have shared access
}

void writer()
{
  // get upgradable access
  boost::upgrade_lock<boost::shared_mutex> lock(_access);

  // get exclusive access
  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
  // now we have exclusive access
}

这篇关于例如升压shared_mutex(多次读取/一次写入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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