Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 471783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:01:15+00:00 2026-05-13T00:01:15+00:00

When I write a value into a field, what guarantees do I get regarding

  • 0

When I write a value into a field, what guarantees do I get regarding when the new value will be saved in the main memory? For example, how do I know that the processor don’t keep the new value in it’s private cache, but updated the main memory?
Another example:

int m_foo;

void Read() // executed by thread X (on processor #0)
{
   Console.Write(m_foo);
}

void Write() // executed by thread Y (on processor #1)
{
   m_foo = 1;
}

Is there a possibility that after Write() was finished executing, some other thread executes Read() but actually will see “0” as the current value? (since perhaps the previous write to m_foo wasn’t flushed yet?).
What kind of primitives (beside locks) are available to ensure the the writes were flushed?


EDIT
In the code sample I’ve used, the write and read are placed at different method. Doesn’t Thread.MemoryBarrier only affect instruction reording that exist in the same scope?

Also, let’s assume that they won’t be inlined by the JIT, how can I make sure that the value written to m_foo won’t be stored in a register, but to the main memory? (or when m_foo is read, it won’t get an old value from the CPU cache).

Is it possible to achieve this without using locks or the ‘volatile’ keyword? (also, let’s say I’m not using primitive types, but a WORD sized structs [so volatile cannot be applied].)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:01:15+00:00Added an answer on May 13, 2026 at 12:01 am

    Volatile and Interlocked have already been mentioned, you asked for primitives, one addition to the list is to use Thread.MemoryBarrier() before your writes or reads. This guarantees no reordering is done of memory writes and reads.

    This is doing “by hand” what lock, Interlocked and volatile can do automatically most of the time. You could use this as a full replacement to any other technique, but it is arguably the hardest path to travel, and so says MSDN:

    “It is difficult to build correct multithreaded programs by using
    MemoryBarrier. For most purposes, the
    C# lock statement, the Visual Basic
    SyncLock statement, and the methods of
    the Monitor class provide easier and
    less error-prone ways to synchronize
    memory accesses. We recommend that you
    use them instead of MemoryBarrier. “

    How to use MemoryBarrier

    A very fine example are the implementations of VolatileRead and VolatileWrite, that both internally use MemoryBarrier. The basic rule of thumb to follow is: when you read a variable, place a memory barrier after the read. When you write the value, the memory barrier must come before the write.

    In case you’ve doubts whether this is less efficient then lock, consider that locking is nothing more then “full fencing”, in that it places a memory barrier before and after the code block (ignoring Monitor for a moment). This principle is well explained in this excellent definitive article on threads, locking, volatile and memory barriers by Albahari.

    From reflector:

    public static void VolatileWrite(ref byte address, byte value)
    {
        MemoryBarrier();
        address = value;
    }
    
    public static byte VolatileRead(ref byte address)
    {
        byte num = address;
        MemoryBarrier();
        return num;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You definitely want your unit test directories to mirror the… May 14, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer Specify the ERRORS SQLLDR command line parameter. May 14, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer I'm guessing its the initial time to compile the page.… May 14, 2026 at 8:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.