c# - Is it safe to share local variable between threads (via a callback closure)? -
I want to do something like the following - basically I'm calling an async operation that calls callback in another thread And I want to wait to complete the "inline". My concern is that the variables that are shared in the thread (bar and event) can not be synchronized due to being stored in the registers, for example if they Members were variables then I Could have marked the stability, but created on the stack can not be used volatility on local variables. I can use member variables, but I think my classroom is not messy by keeping its cleaner all this local.
repeatedly = null; Manual Reset Event Event = New Manual Retset (Incorrect); Foo.AsyncOperation (New Action ((=) = & gt; {// This representative will be called in another thread bar = ... event.Set ();}); event.WaitOne (timeout); // Usage Bar
Yes, this will work correctly read here
The following implied complete fences generate: waiting for the signal to be created and in the signaling Manual Retset is included . If you want to know if there is a full fence , Then in the same page: The simplest type of complete fence memory barrier is a complete memory barrier (full fence) that can sort any type of instruction or prevent caching around that fence The calling thread generates a complete fence;
Comments
Post a Comment