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

  • Home
  • SEARCH
  • 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 6739009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:26:12+00:00 2026-05-26T11:26:12+00:00

While I was learning threading memory barrier (fences) seems really not easy to understand,

  • 0

While I was learning threading memory barrier (fences) seems really not easy to understand, here in my case I want employee 10 threads simultaneously increase a Int32 number: x by 100 times at each (x++), and will get result 10 * 100 = 1000.

So this is actually an atomicity problem, and what I know so far there are a number of ways to achieve that – limited in concurrent ways:

  1. Interlocked.Increment
  2. exclusive lock (lock, monitor, Mutex, Semaphore, etc.)
  3. ReadWriteLockSlim

If there are more better ways please kindly guide me, I tried to use a volatile read/write but failed:

for (int i = 0; i < 10000; i++)
{
    Thread.VolatileRead(ref x);
    Thread.VolatileWrite(ref x, x + 1);
}

My investigation code is tidied below:

private const int MaxThraedCount = 10;
private Thread[] m_Workers = new Thread[MaxThraedCount];
private volatile int m_Counter = 0;
private Int32 x = 0;

protected void btn_DoWork_Click(object sender, EventArgs e)
{
    for (int i = 0; i < MaxThraedCount; i++)
    {
        m_Workers[i] = new Thread(IncreaseNumber) { Name = "Thread " + (i + 1) };
        m_Workers[i].Start();
    }
}

void IncreaseNumber()
{
    try
    {
        for (int i = 0; i < 10000; i++)
            Interlocked.Increment(ref x);

        // Increases Counter and decides whether or not sets the finish signal
        m_Counter++;
        if (m_Counter == MaxThraedCount)
        {
            // Print finish information on UI
            m_Counter = 0;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}

My question is: how can I use Memory Barrier to replace Interlocked, since “All of Interlocked’s methods generate a full fence.“, I tried to modify the increase loop as below but failed, I don’t understand why…

for (int i = 0; i < 10000; i++)
{
    Thread.MemoryBarrier();
    x++;
    Thread.MemoryBarrier();
}
  • 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-26T11:26:12+00:00Added an answer on May 26, 2026 at 11:26 am

    The memory barrier just keeps memory operations from moving from one side of the barrier to the other. Your issue is this:

    1. Thread A reads the value of X.
    2. Thread B reads the value of X.
    3. Thread A adds one to the value it read.
    4. Thread B adds one to the value it read.
    5. Thread A writes back the value it calculated.
    6. Thread B writes back the value it calculated.

    Oops, two increments only added one. Memory barriers are not atomic operations and they are not locks. They just enforce ordering, not atomicity.

    Unfortunately, the x86 architecture does not offer any atomic operations that don’t include a full fence. It is what it is. On the bright side, the full fence is heavily optimized. (For example, it does not ever lock any bus.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While learning about java memory profiling, I keep seeing the term perm space in
It hasn't been long since I started learning python, but I really want to
While learning Haskell I had the feeling that the authors where not always telling
While learning different languages, I've often seen objects allocated on the fly, most often
i just got some more questions while learning PHP, does php implement any built
MOV is probably the first instruction everyone learns while learning ASM. Just now I
I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it
So I found some similarities between arrays and set notation while learning about sets
HI there! I've run into some problem while learning to combine .sh files and
Sorry since this question is specific to my problem. While learning reflections i did

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.