Will interlocked operation only prevent other interlocked operation on the same location or any visit to the same location?

when reading
https://msdn.microsoft.com/en-us/magazine/cc163715.aspx , i found the following statement:
interlocked operations guarantee that the update is atomic. This requires the processor to insure that no other processor is also trying to execute an interlocked operation on the same location at the same time.
my question is, what if other thread is trying to read the same location at the same time without using a interlocked operation, will an interlocked operation prevent this?
I was using a readerwriter lock to sync my threads operation on an object. and i found interlocked.compareexchange is handy to initialize the object: interlocked.compareexchange(ref object, FunctionThatReturnAnIntializedObject(),null). However, i am not
sure whether other threads can read that object during the interlocked operation? If they can, how can i prevent them?

"However, i am not sure whether other threads can read that object during the interlocked operation? If they can, how can i prevent them?"
If I understand correctly you're trying to lazy initialize a variable.
Why aren't you using Lazy<T> to begin with, it does exactly what you want if you use LazyThreadSafetyMode.PublicationOnly.
But to answer to your question: mixing interlocked and non-interlocked accesses is problematic. There are a lot of cases where doing this won't work and some cases where it works.
In your particular case this interlocked/non-interlocked mix should work fine. In addition to being atomic .NET's interlocked operations are also supposed to be full fences. A full fence prevents other read/writes from being moved before/after the interlocked
operation and that's exactly what you need to prevent the writes required to initialize the object from being moved after interlocked.
If the thread that doesn't use interlocked to read the object variable gets a non-null reference then it means that the initialization of the object was done, otherwise it couldn't have got a non-null reference in the first place.
There is a small chance to run into a problem caused by compiler optimizations. If the thread that doesn't use interlocked has a loop like the following then it's possible that the loop will never end.
while (object == null) { ... }
That's because in the absence of volatile and interlocked the compiler can read "object" into a register before the loop. The loop test will keep comparing the register value with null and the loop will never end. Such a situation is rather unlikely
but you should make the object variable volatile to avoid this.

Similar Messages

Maybe you are looking for

  • The line to type in under the tabs has disappeared. How do I get it back?

    The line under the tabs-where I could type in a question, web site or view the web site address is missing. How do I get it back?

  • How to migrate attachments from legacy system to SAP.

    Hello experts,                         We have a requirement to migrate attachments from a legacy application to SAP .Attachments are in the form of a PDF, xls, powerpoints. These are to be attached to accounts, opportunities etc.    Please provide y

  • Grid in Oracle Forms

    wil it be possiblt to create a form where we can drag the change the width of the columns in the form. How can this be accomplished?

  • Evince and mime types

    Since the last update of evince to 0.83 it won't open pdf files no more - I get: Unable to open document Unhandled MIME type: "application/pdf" Is this a bug? I can open pdfs using kpdf or kghostview without a problem.

  • Dispatch Event for loaded Netconnection datas

    Hi All! I made a manager component to retrieve data from database through amfphp. When the datas are loaded at the end of the "onResult" function, I dispatch an event "contactLoader" Another parent mxml component has an instance of my manager compone