3

If I have a code similar to this:

ManualResetEvent me = new ManualResetEvent(false);
. . .
int a = 10;
. . .
me.WaitOne();
. . .

assume a is in the CPU cache after the assignment, will call to WaitOne flush it to the RAM?

Or (more importantly)

will an access to a kernel sync object (like WaitHangle) from .NET result in CPU cache flush / memory barrier?

Or

does switch to kernel mode require memory barrier?

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • Interesting question, but: it probably depends on *what is `a`* - because in the example given, the compiler will typically remove it as an unused variable - and even if it was used, since no other threads can see it (as a register/stack value) it is hard to answer meaningfully. Would probably need to be a field to be a concern. – Marc Gravell Feb 17 '13 at 13:53
  • @MarcGravell I have asked this 3 different ways to avoid being specific on 'a'. I need to know will the CPU cache be flushed before switching to kernel mode (regardless the 'a' origin - there will be lot of stuff in the cache not just 'a' and not just from this thread or process.. right?) – Boppity Bop Feb 17 '13 at 14:09
  • 1
    Possibly answered here: http://stackoverflow.com/questions/681505/does-an-eventwaithandle-have-any-implicit-memorybarrier, in particular this answer: http://stackoverflow.com/a/10651601/23354 – Marc Gravell Feb 17 '13 at 14:22
  • if you read the answer - there is a lot of 'I think' and contradictions (like he is saying Richter said yes and I think no etc).. Plus it is very specific to the calling method Set on the specific WaitHandle... (hence the answer applies to the implementation of a piece of code)... my question is - generally speaking is there a link between switching .NET app to kernel-mode and ensuring CPU cache coherency – Boppity Bop Feb 17 '13 at 14:56
  • 2
    Possibly answered here: http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained. WaitOne has acquire semantics, so cache will be refreshed but not flushed – Andriy Vandych Nov 18 '13 at 18:48
  • finally. someone who knows something about the topic and not fishing for the SO score! perhaps in 15 yrs time this will be place for intelligent exchange of knowledge and NOT a horse races betting for nerds.. – Boppity Bop Nov 18 '13 at 20:05
  • Are you looking for "might my variable be flushed to memory?" or "can I guarantee that my variable will be flushed to memory?" And are you looking at "Microsoft's .NET on x86," or .NET in general on any platform with potentially emulated kernel calls? – Cort Ammon Nov 29 '14 at 07:25
  • I am looking at x64.. I didn't know that changes behavior. whats `emulated kernel calls` ? – Boppity Bop Nov 29 '14 at 14:21

0 Answers0