Does a thread start have implicit memory barrier?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 12:28:49

问题


i.e. when I start a new thread, is it safe to read any variables initialized before the thread start from the newly started thread?

UPD why I'm asking? Because some developers assume that it does have, and don't use any explicit memory barriers there. And I couldn't reproduce any bugs in that code. However, documentation http://msdn.microsoft.com/en-us/library/ms686355(v=vs.85).aspx doesn't say this is safe.


回答1:


Yes, the operating system provides the implicit barrier. It can't get the thread started without taking an internal lock that protects the thread scheduler data structures.

Which isn't explicitly promised that I know of. Since .NET 2.0, there is no longer a direct correspondence between a Thread and a ProcessThread. This link was broken at the request of the SQL Server team that wanted to implement a Thread with a fiber. This did not actually happen, they gave up on the project when they couldn't make it reliable enough. Given the outcome of this project and no mainstream project I know of that has ever tried to take advantage of this again, as well as the difficulty of implementing Thread support that doesn't use a lock, I'd say it is safe to assume you can rely on the implicit barrier.



来源:https://stackoverflow.com/questions/10672495/does-a-thread-start-have-implicit-memory-barrier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!