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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:21:37+00:00 2026-05-24T00:21:37+00:00

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html I believe that the following code increases the value of var atomically. volatile

  • 0

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html

I believe that the following code increases the value of var atomically.

volatile int var = 0;
__sync_fetch_and_add( &var, 1 )

I understood the above codes as the following logic:

  1. Load the address of variable var
  2. write number 1 onto variable var atomically – through register/cache, somehow

However, I doubt if the following is atomic, too

volatile int var = 0;
volatile int num = 1;
__sync_fetch_and_add( &var, num )

Since it may be interpreted as

  1. Load the address of variable var
  2. Load the value of variable num into a register
  3. write the value onto variable var.

After #2 is executed, but before #3,
the CPU/thread gets interrupted and another CPU/thread updates
the value of variable num.

In other words,
when using _sync*() of gcc,
can I use a variable, not a constant, as the second argument?

Doesn’t it break the atomicity?

  • 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-24T00:21:38+00:00Added an answer on May 24, 2026 at 12:21 am

    The operation is really two operations.

    __sync_fetch_and_add( &var, num )
    

    Loading num is atomic. Adding it to var is atomic. But two atomic operations do not make an atomic operation when put together. This is why it is so hard to invent new lock-free data structures. In general, two thread-safe operations do not necessarily make a thread-safe operation when composed. This is the reason why it is so difficult to make correct multithreaded applications.

    You see, __sync_fetch_and_add is indeed atomic, but it behaves like an ordinary function — so it takes the current value of “num” as a parameter. It is not quite correct to say the atomicity of the function is broken — because it is the responsibility of the caller to load the value from num, and it’s not part of the function’s interface. I could equally complain about this:

    __sync_fetch_and_add(&var, some_really_long_function());
    

    Or worse,

    __sync_fetch_and_add(long_function_1(), long_function_2());
    

    You say it “may be interpreted as”

    1. Load the address of variable var
    2. Load the value of variable num
    3. Perform the atomic addition

    But according to the C spec, it’s not that it may be interpreted this way, but rather, it must be interpreted this way, otherwise the compiler would not be conformant (actually, it could swap #1 and #2, but that’s not important here).

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

Sidebar

Related Questions

So I was reading http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html , and came across this: type __sync_and_and_fetch (type *ptr,
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/objc-features_1.html#SEC2 The GNU Objective-C runtime provides a way that allows you to execute code
I was looking at libstdc++ documentation at http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01618.html and found it arranged in modules
This page says that GCC 4.5 has C++ raw string literals: http://gcc.gnu.org/projects/cxx0x.html But when
All the GNU gcov manual online at http://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov says about gcov -f option is:
I found Ofast level opt in the doc of gcc on http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options . But
From this link: http://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html If a standard system include directory, or a directory specified
I could not find any intrinsics for a simple xor operation. See: http://gcc.gnu.org/onlinedocs/gcc/ARM-NEON-Intrinsics.html Are
I'm currently trying to get g++ working, and looking at http://gcc.gnu.org/install/build.html , I can't
http://www.gnu.org/s/libmicrohttpd/tutorial.html#Hello-browser-example gcc helloworld.c -lmicrohttpd -I/opt/local/include/ helloworld.c: In function ‘answer_to_connection’: helloworld.c:18: warning: incompatible implicit declaration

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.