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 6379479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:08:36+00:00 2026-05-25T02:08:36+00:00

In this threading online book: http://www.albahari.com/threading/part4.aspx theres an example of Thread.MemoryBarrier() class Foo {

  • 0

In this threading online book: http://www.albahari.com/threading/part4.aspx

theres an example of Thread.MemoryBarrier()

   class Foo
{
  int _answer;
  bool _complete;

  void A()
  {
    _answer = 123;
    Thread.MemoryBarrier();    // Barrier 1
    _complete = true;
    Thread.MemoryBarrier();    // Barrier 2
  }

  void B()
  {
    Thread.MemoryBarrier();    // Barrier 3
    if (_complete)
    {
      Thread.MemoryBarrier();       // Barrier 4
      Console.WriteLine (_answer);
    }
  }
}

We got a discussion whether there is any thread blocking going on or not?

Im thinking there is some, especially given that

A full fence takes around ten nanoseconds on a 2010-era desktop.

On other hand, full fence is only supposed to disable instructions reodering and caching which by its sound doesn’t qualify as thread blocking, (unlike lock where its clear that thread waits for other to release lock before it continues, and is blocked during that time)

About that thread ‘block state’. im talking not in terms of whether thread is put into blocked state or not, but whether there is some thread synchronization happening, which means one thread is not able to run while other isn’t letting it to do so, by means of MemoryBarrier in this case.

Also Id like to get clear understanding what each barrier achieves. For example Barrier 2 – how exactly it provides freshness guarantee and how is it connected to barrier 3? If someone would explain in detail whats each barrier purpose here( what could possibly go wrong if 1 or 2 or 3 or 4 weren’t there) I think id improve my understanding of this greatly.

EDIT: its mostly clear now what 1, 2, and 3 do. However what 4 does that 3 doesn’t is still unclear.

  • 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-25T02:08:37+00:00Added an answer on May 25, 2026 at 2:08 am

    The fact that instructions take time to execute does not imply that a thread is blocked. A thread is blocked when it is specifically put into a blocked state, which MemoryBarrier() does not do.

    The processor instructions that actually prevent instruction reordering and cache flushing take time, because they must wait for the caches to become coherent again. During that time, the thread is still considered running.

    Update: So let’s take a look at what’s actually happening in the example, and what each memory barrier actually does.

    As the link says, 1 and 4 ensure that the correct answers are produced. That’s because 1 ensures that the answers are flushed into memory, and 4 ensures that the read caches are flushed prior to retrieving the variables.

    2 and 3 ensure that if A runs first, then B will always print the answers. Barrier 2 ensures that the write of true is flushed to memory, and barrier 3 ensures that the read cahces are flushed before testing _complete‘s value.

    The cache and memory flushing should be clear enough, so let’s look at instruction reordering. The way the compiler, CLR and CPU know they can reorder instructions is by analyzing a set of instructions in sequence. When they see the barrier instruction in the middle of a sequence, they know that instructions can’t move across that boundary. That ensures that in addition to cache freshness, the instructions occur in the correct order.

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

Sidebar

Related Questions

I am doing something like this in python class MyThread ( threading.Thread ): def
I have this class: from threading import Thread import time class Timer(Thread): def __init__(self,
This is my code: class facebookConnection { // Allow multi-threading. private $_mch = NULL;
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Work on this small test application to learn threading/locking. I have the following code,
I am using multi threading concept to run some process. this process uses the
I want to code a trading bot for Magic: The Gathering Online . This
I may be greatly misunderstanding this threading scenario, but that's why I'm asking. What
I just wrote this code: System.Threading.SynchronizationContext.Current.Post( state => DoUpdateInUIThread((Abc)state), abc); but System.Threading.SynchronizationContext.Current is null
I am new to thread programming in Java. To understand threading I'm trying to

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.