一位读者.一位作家.有关互斥体和原子内建函数的一些一般性问题 [英] One reader. One writer. Some general questions about mutexes and atomic-builtins

查看:79
本文介绍了一位读者.一位作家.有关互斥体和原子内建函数的一些一般性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父线程和一个工作线程,它们共享一个bool标志和一个std :: vector.父级仅读取(即读取bool或调用my_vector.empty());工人只写.

I have a parent and a worker thread that share a bool flag and a std::vector. The parent only reads (i.e., reads the bool or calls my_vector.empty()); the worker only writes.

我的问题:

  • 我需要互斥保护bool标志吗?

  • Do I need to mutex protect the bool flag?

我可以说所有布尔值读/写本质上都是原子操作吗?如果您说是"或否",那么您是从哪里获得信息的?

Can I say that all bool read/writes are inherently atomic operations? If you say Yes or No, where did you get your information from?

我最近听说了 GCC原子内置 .我可以使用这些使我的标志读/写原子,而不必使用互斥体吗?有什么区别?我了解Atomic内置函数可以归结为机器代码,但是即使互斥锁也可以归结为CPU的内存屏障指令,对吗?人们为什么将互斥锁称为"OS级"结构?

I recently heard about GCC Atomic-builtin. Can I use these to make my flag read/writes atomic without having to use mutexes? What is the difference? I understand Atomic builtins boil down to machine code, but even mutexes boil down to CPU's memory barrier instructions right? Why do people call mutexes an "OS-level" construct?

我需要互斥保护我的std :: vector吗?回想一下,工作线程填充了此向量,而父线程仅在其上调用empty()(即,仅读取它)

Do I need to mutex protect my std::vector? Recall that the worker thread populates this vector, whereas the parent only calls empty() on it (i.e., only reads it)

我认为互斥保护对于bool或vector都不是必需的.我进行如下合理化处理:好吧,如果我在更新共享内存之前就读了它.那还是可以的,下次我将获得更新后的值.更重要的是,我看不到为什么在写程序时应该阻止写程序阅读就是阅读,因为毕竟读者只是阅读!"

I do not believe mutex protection is necessary for either the bool or the vector. I rationalize as follows, "Ok, if I read the shared memory just before it was updated.. thats still fine, I will get the updated value the next time around. More importantly, I do not see why the writer should be blocked while the reading is reading, because afterall, the reader is only reading!"

如果有人可以指出我正确的方向,那就太好了.我使用的是GCC 4.3和32位Intel x86. 非常感谢!

If someone can point me in the right direction, that would be just great. I am on GCC 4.3, and Intel x86 32-bit. Thanks a lot!

推荐答案

我需要互斥保护bool标志吗?

Do I need to mutex protect the bool flag?

不一定要执行原子指令. atomic instruction是指编译器固有函数,a)防止编译器重新排序/优化,b)导致原子读/写,c)发出适当的内存隔离区以确保CPU之间的可见性(对于使用 MESI缓存一致性协议).类似于 gcc原子内置函数.

Not necessarily, an atomic instruction would do. By atomic instruction I mean a compiler intrinsic function that a) prevents compiler reordering/optimization and b) results in atomic read/write and c) issues an appropriate memory fence to ensure visibility between CPUs (not necessary for current x86 CPUs which employ MESI cache coherency protocol). Similar to gcc atomic builtins.

我可以说所有布尔读/写本来就是原子操作吗?如果您说是"或否",那么您是从哪里获得信息的?

Can I say that all bool read/writes are inherently atomic operations? If you say Yes or No, where did you get your information from?

取决于CPU.对于Intel CPU-是的.请参阅英特尔®64和IA-32架构软件开发人员手册.

Depends on the CPU. For Intel CPUs - yes. See Intel® 64 and IA-32 Architectures Software Developer's Manuals.

我最近听说了GCC Atomic-builtin.我可以使用这些使我的标志读/写原子,而不必使用互斥体吗?有什么区别?我了解Atomic内置函数可以归结为机器代码,但是即使互斥锁也可以归结为CPU的内存屏障指令,对吗?人们为什么将互斥锁称为"OS级"构造?

I recently heard about GCC Atomic-builtin. Can I use these to make my flag read/writes atomic without having to use mutexes? What is the difference? I understand Atomic builtins boil down to machine code, but even mutexes boil down to CPU's memory barrier instructions right? Why do people call mutexes an "OS-level" construct?

原子和互斥锁之间的区别在于,后者可以使等待线程休眠,直到互斥锁释放为止.使用原子,您只能忙于自旋.

The difference between atomics and mutexes is that the latter can put the waiting thread to sleep until the mutex is released. With atomics you can only busy-spin.

我需要互斥保护我的std :: vector吗?回想一下,工作线程填充了此向量,而父线程仅在其上调用empty()(即仅读取它)

Do I need to mutex protect my std::vector? Recall that the worker thread populates this vector, whereas the parent only calls empty() on it (i.e., only reads it)

你知道.

我认为互斥保护对于bool或vector都不是必需的.我进行如下合理化处理:好吧,如果我在更新共享内存之前就读了它.那还是可以的,下次我将获得更新后的值.更重要的是,我看不到为什么在写程序时应该阻止写程序阅读就是阅读,因为毕竟读者只是阅读!"

I do not believe mutex protection is necessary for either the bool or the vector. I rationalize as follows, "Ok, if I read the shared memory just before it was updated.. thats still fine, I will get the updated value the next time around. More importantly, I do not see why the writer should be blocked while the reading is reading, because afterall, the reader is only reading!"

根据实现的不同,vector.empty()可能涉及读取两个缓冲区的开始/结束指针并对其进行减法或比较,因此有可能您读取了一个指针的新版本而没有互斥量的另一个指针的旧版本. .可能会出现令人惊讶的行为.

Depending on the implementation, vector.empty() may involve reading two buffer begin/end pointers and subtracting or comparing them, hence there is a chance that you read a new version of one pointer and an old version of another one without a mutex. Surprising behaviour may ensue.

这篇关于一位读者.一位作家.有关互斥体和原子内建函数的一些一般性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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