1字节共享内存需要Mutex [英] Is Mutex required for 1 byte shared memory

查看:160
本文介绍了1字节共享内存需要Mutex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的案例是一个线程读取,并想要
决定是否需要更改该值。



某些事情如下

  void set(bool status)
{
if(status == m_status)
return;
monitor.lock();
m_status = status;

}

如果可能?




在Windows上,您可以使用联锁的变量访问。 p>

有关跨平台解决方案,请参阅 Boost Atomic



std :: atomic 也是一个解决方案


my case is one thread read and want to decide if needed to change the value or not?

some thing like below

void set(bool status)
{ 
    if(status == m_status)
         return;
     monitor.lock();
     m_status = status;

}

if this possible?

解决方案

Using a synchronization object for boolean state is overkill.

On Windows you can use Interlocked Variable Access.

For cross platform solution .. see Boost Atomic

std::atomic from C++11 is also a solution

这篇关于1字节共享内存需要Mutex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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