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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:04:03+00:00 2026-05-12T10:04:03+00:00

I need to provide synchronization to some members of a structure. If the structure

  • 0

I need to provide synchronization to some members of a structure.
If the structure is something like this

struct SharedStruct {
    int Value1;
    int Value2;
}

and I have a global variable

SharedStruct obj;  

I want that the write from a processor

 obj.Value1 = 5; // Processor B

to be immediately visible to the other processors, so that when I test the value

 if(obj.Value1 == 5) { DoSmth(); } // Processor A
 else DoSmthElse();   

to get the new value, not some old value from the cache.
First I though that if I use volatile when writing/reading the values, it is enough. But I read that volatile can’t solve this kind o issues.
The members are guaranteed to be properly aligned on 2/4/8 byte boundaries, and writes should be atomic in this case, but I’m not sure how the cache could interfere with this.
Using memory barriers (mfence, sfence, etc.) would be enough ? Or some interlocked operations are required ?
Or maybe something like

lock mov addr, REGISTER  

?
The easiest would obviously be some locking mechanism, but speed is critical and can’t afford locks 🙁

Edit
Maybe I should clarify a bit. The value is set only once (behaves like a flag). All the other threads need just to read it. That’s why I think that it may be a way to force the read of this new value without using locks.

Thanks in advance!

  • 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-12T10:04:03+00:00Added an answer on May 12, 2026 at 10:04 am

    All the other answers here seem to hand wave about the complexities of updating shared variables using mutexes, etc. It is true that you want the update to be atomic.
    And you could use various OS primitives to ensure that, and that would be good
    programming style.

    However, on most modern processors (certainly the x86), writes of small, aligned scalar values is atomic and immediately visible to other processors due to cache coherency.
    So in this special case, you don’t need all the synchronizing junk; the hardware does the
    atomic operation for you. Certainly this is safe with 4 byte values (e.g., “int” in 32 bit C compilers).

    So you could just initialize Value1 with an uninteresting value (say 0) before you start the parallel threads, and simply write other values there. If the question is exiting the loop on a fixed value (e.g., if value1 == 5) this will be perfectly safe.

    If you insist on capturing the first value written, this won’t work. But if you have a parallel set of threads, and any value written other than the uninteresting one will do, this is also fine.

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

Sidebar

Related Questions

I need to provide some passwords, API keys and similar sensitive data in my
I have a few large forms that I need to provide visual cues about
I have a wcf web service and I need to provide the client with
I have Text( http://www.mywebsite.com ) in a view, in which i need to provide
I need to provide some functionality to another site. What way is better and
I need to provide webpage (made for desktop browsers) to be usable on mobile
I need to provide our web developers an easy and quick way to test
On the form I need to provide the validation where the validation depends on
To create facebook android native app we need to provide our Android application signature
I'm currently writing an app in Python and need to provide localization for it.

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.