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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:22:41+00:00 2026-06-06T21:22:41+00:00

I have a variable in shared memory x on a multi- processor system. void

  • 0

I have a variable in shared memory x on a multi-processor system.

void MyFunction(volatile int* x) {
  if (*x != 0) {
     // do something
  }
}

Other processes (possibly on different processors) will be writing to x using gcc built-in atomic operations such as __sync_bool_compare_and_swap etc.

I think I’m running into some cache concurrency issues where sometimes it takes a bit of time before x finally gets updated with the new value.

What I want is a kind of atomic_compare (without the swap), if such a thing exists? Or an “atomic read”. What’s the fastest way to do this? (avoiding mutexes, locks, etc.)

Thanks

Edit:

I just realized a somewhat hackish workaround would be to use __sync_val_compare_and_swap with a value that I knew it could never be. Would that solve the issue? (Is there a cleaner way?)

  • 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-06-06T21:22:43+00:00Added an answer on June 6, 2026 at 9:22 pm

    The new C standard, C11, has _Atomic data types and operations to deal with this. This standard is not yet implemented, but gcc and clang are close to it, they already implement the functionality. And in fact the function __sync_bool_compare_and_swap is part of it. I have wrapped that into a set of headers in P99 that let you program already with the C11 interfaces.

    The C11 function to do what you want would be atomic_load or if you have particular requirements for the coherence atomic_load_explicit. And no surprise, as you suspected, P99 maps that on __sync_val_compare_and_swap(&x, 0, 0). Then if you look into the assembler that this generates on most architectures this will just translate in a simple load operation in the case of x beeing an int. But this is not guaranteed by the language, it is up to the compiler to know such things and to synthesize the instructions that are guaranteed to be atomic.

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

Sidebar

Related Questions

I have a variable named as [ThreadStatic] , will it shared across 2 app
I have a component that has a sub-component they both use a shared variable
I have variable which I populate from JSON formated data. Something like: var time=my_data[data.results[i].id].time;
I have variable @count of datatype int.I am setting values to this @count. I
Let's say we have an array defined as a global variable. int array[] =
Here I have: class X { public: static int shared_arr[]; static void alloc_and_init() {
I have a class which opens a posix shared memory using shm_open . Now
Question: How can I implement shared memory variable in PHP without the semaphore package
I got a bit confused: In production we have two processes communicating via shared
Assume we have a multithreaded C program (pthreads), and the (unsynchronized) shared variable accesses

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.