码迷,mamicode.com
首页 > 编程语言 > 详细

c# 线程同步问题(about volatile)

时间:2017-09-04 17:45:52      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:opera   ocs   library   touch   get   线程   upd   ever   lin   

这个古老的话题,

首先让我们看stackoverflow中一个经典的问题:

Volatile vs. Interlocked vs. lock

 

然后连接看答案:

https://stackoverflow.com/questions/154551/volatile-vs-interlocked-vs-lock

这里的误区,主要是对volatile的解释和使用;

关于volatile的解释和示例:

http://www.jaylee.org/post/2004/08/05/Dont-get-C-volatile-the-wrong-way.aspx

 

这里还有一篇较好的文章:

http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/ (这篇文章讲得非常好,非常好,强烈推荐)

一些文字摘要:

The memory model is a fascinating topic – it touches on hardware, concurrency, compiler optimizations, and even math.

The memory model defines what state a thread may see when it reads a memory location modified by other threads. For example, if one thread updates a regular non-volatile field, it is possible that another thread reading the field will never observe the new value. This program never terminates (in a release build):

 

这里我们再进行  synchronization  中 Barrier 的使用;

 

https://msdn.microsoft.com/en-us/library/system.threading.barrier(v=vs.110).aspx (微软官网示例方法)

它的另外一种用法滴啊;

How to: Synchronize Concurrent Operations with a Barrier

https://docs.microsoft.com/en-us/dotnet/standard/threading/how-to-synchronize-concurrent-operations-with-a-barrier

 

还有这个例子:

http://dotnetpattern.com/threading-barrier

 

c# 线程同步问题(about volatile)

标签:opera   ocs   library   touch   get   线程   upd   ever   lin   

原文地址:http://www.cnblogs.com/mc67/p/7474345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!