互斥锁定功能是否足够,没有易失性? [英] Are mutex lock functions sufficient without volatile?

查看:143
本文介绍了互斥锁定功能是否足够,没有易失性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事和我为在x86,x64,Itanium,PowerPC和其他10年服务器CPU上运行的各种平台编写软件。



讨论关于互斥量函数(如pthread_mutex_lock()... pthread_mutex_unlock()是否足够,或者受保护变量是否需要是易变的)。

  int foo :: bar()
{
// ...
//可能访问_protected或不访问_protected的代码。
pthread_mutex_lock(m);
int ret = _protected;
pthread_mutex_unlock(m);
return ret;
}

我关心的是缓存。编译器可以在栈上或寄存器中放置_protected的副本,并在赋值中使用该陈旧的值吗?如果不是,什么阻止这种情况发生?这种模式的变体是否易受攻击?



我假设编译器实际上并不理解pthread_mutex_lock()是一个特殊函数,所以我们只受序列点的保护吗?



非常感谢。



更新:好的,我可以看到一个趋势,我尊重那些答案,但关于这个问题的文章很容易在网上找到。我在网上找不到,我提出这个问题的原因是,我如何保护没有 volatile。 如果上述代码正确,如何无法缓存问题?

解决方案
如果上面的代码是正确的,那么如何缓存
问题?


直到C ++ 0x,它不是。它并没有在C中指定。所以,它真的取决于编译器。一般来说,如果编译器不能保证它将遵守对涉及多个线程的函数或操作的存储器访问的排序约束,那么您将无法使用该编译器编写多线程安全代码。请参阅Hans J Boehm的主题无法实现为图书馆



至于你的编译器应该支持什么抽象线程安全的代码,在内存障碍的wikipedia条目是一个相当不错的起点。



(至于为什么人们建议 volatile volatile 作为编译器的内存屏障,绝对不是标准。)


A coworker and I write software for a variety of platforms running on x86, x64, Itanium, PowerPC, and other 10 year old server CPUs.

We just had a discussion about whether mutex functions such as pthread_mutex_lock() ... pthread_mutex_unlock() are sufficient by themselves, or whether the protected variable needs to be volatile.

int foo::bar()
{
 //...
 //code which may or may not access _protected.
 pthread_mutex_lock(m);
 int ret = _protected;
 pthread_mutex_unlock(m);
 return ret;
}

My concern is caching. Could the compiler place a copy of _protected on the stack or in a register, and use that stale value in the assignment? If not, what prevents that from happening? Are variations of this pattern vulnerable?

I presume that the compiler doesn't actually understand that pthread_mutex_lock() is a special function, so are we just protected by sequence points?

Thanks greatly.

Update: Alright, I can see a trend with answers explaining why volatile is bad. I respect those answers, but articles on that subject are easy to find online. What I can't find online, and the reason I'm asking this question, is how I'm protected without volatile. If the above code is correct, how is it invulnerable to caching issues?

解决方案

If the above code is correct, how is it invulnerable to caching issues?

Until C++0x, it is not. And it is not specified in C. So, it really depends on the compiler. In general, if the compiler does not guarantee that it will respect ordering constraints on memory accesses for functions or operations that involve multiple threads, you will not be able to write multithreaded safe code with that compiler. See Hans J Boehm's Threads Cannot be Implemented as a Library.

As for what abstractions your compiler should support for thread safe code, the wikipedia entry on Memory Barriers is a pretty good starting point.

(As for why people suggested volatile, some compilers treat volatile as a memory barrier for the compiler. It's definitely not standard.)

这篇关于互斥锁定功能是否足够,没有易失性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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