如果第二个线程等待第一个线程的终止,是否需要内存屏障? [英] Is a memory barrier required if a second thread waits for termination of the first one?

查看:24
本文介绍了如果第二个线程等待第一个线程的终止,是否需要内存屏障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设线程 Alpha 正在写入变量 A 而没有锁定.第二个线程 Beta 正在等待 Alpha 终止,然后依次读取变量 A.

Suppose that thread Alpha is writing to variable A without locking. A second thread Beta is waiting for Alpha to terminate, then reads the variable A in turn.

A的内容会不会是新鲜的?内存写入可以延迟到线程生命周期之外吗?等待线程 Alpha 终止的标准机制不会隐含地充当内存屏障吗?

Is it possible that the contents of A will not be fresh? Can memory writes be delayed beyond the thread lifetime? Won't the standard mechanism of waiting for thread Alpha termination implicitly work as a memory barrier?

更新 1

有没有不包括内存屏障的等待示例?

Are there any examples of waiting which does not include a memory barrier?

推荐答案

几乎可以肯定(用于等待线程终止的 API 需要为自己的目的使用内存屏障),但我认为你会得到一个明确的答案需要谈谈正在使用的特定线程API.

Almost certainly (the API used to wait for thread termination would need to use memory barriers for its own purposes), but I think for a definitive answer you'll need to talk about the specific threading API being used.

例如,posix 为 pthread_join() 做出这样的保证:https://stackoverflow.com/a/3208140/12711

For example, posix makes such a guarantee for pthread_join(): https://stackoverflow.com/a/3208140/12711

Win32 记录了等待对象(例如,线程句柄)的同步 API 强加了内存屏障:http://msdn.microsoft.com/en-us/library/ms686355.aspx

And Win32 documents that it's synchronization APIs to wait on an object (for example, a thread handle) impose memory barriers: http://msdn.microsoft.com/en-us/library/ms686355.aspx

这篇关于如果第二个线程等待第一个线程的终止,是否需要内存屏障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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