是否锁定确保读取和写入缓存从刷新?如果是这样,怎么样? [英] Does locking ensure reads and writes are flushed from caches? If so, how?

查看:134
本文介绍了是否锁定确保读取和写入缓存从刷新?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读这MSDN文章rel=\"nofollow\">。这篇文章似乎只要访问共享变量之前输入锁推断,那么这些变量将是最新的(.NET 2.0中至少)。

I was reading this MSDN article on lockless thread syncing. The article seems to infer that as long as you enter a lock before accessing shared variables, then those variables will be up to date (in .Net 2.0 at least).

我开始思考如何这是可能的?在.NET中的锁只是一些任意对象,所有的线程访问内存前检查,但锁本身并没有被访问的内存位置的知识。

I got to thinking how this was possible? A lock in .Net is just some arbitrary object that all threads check before accessing memory, but the lock itself has no knowledge of the memory locations that are being accessed.

如果我有一个线程更新变量或内存甚至整个大块,如何保证这些更新是从CPU缓存进入/退出的锁时刷新? ?是否所有内存访问有效的锁内取得挥发性

If I have a thread updating a variable, or even a whole chunk of memory, How are those updates guaranteed to be flushed from CPU caches when entering / exiting a lock? Are ALL memory accesses effectively made volatile inside the lock?

推荐答案

检查埃里克利珀的工作:的 HTTP://博客。 msdn.com/b/ericlippert/archive/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three.aspx

Check the work of Eric Lippert: http://blogs.msdn.com/b/ericlippert/archive/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three.aspx

锁保证存储器读取或修改内部锁被观察到是一致的,锁保证只有一个线程在一个时间访问的存储器的给定大块,等

Locks guarantee that memory read or modified inside the lock is observed to be consistent, locks guarantee that only one thread accesses a given hunk of memory at a time, and so on.

所以,是的,只要你访问共享资源前,每次锁,你可以确信其最新的

So yes, as long as you lock each time before accessing shared resources, you can be pretty sure its up to date

修改查找更多信息和一个非常有用的概述如下岗位:的 http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/

EDIT look up the following post for more information and a very usefull overview: http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/

这篇关于是否锁定确保读取和写入缓存从刷新?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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