Atomic variables in C

I would like to use atomic variables in C.

I tried the following gcc built-in functions, but got a communication error undefined reference to `_sync_fetch_and_add' .

  type __sync_fetch_and_add (type *ptr, type value); type __sync_fetch_and_sub (type *ptr, type value); type __sync_fetch_and_or (type *ptr, type value); type __sync_fetch_and_and (type *ptr, type value); type __sync_fetch_and_xor (type *ptr, type value); type __sync_fetch_and_nand (type *ptr, type value); 

I assume that my architecture does not support them. I thought it was probably because it was not INTEL, but looking at the CPU information, I found that I have an Intel processor.

  >less /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 26 model name : Intel(R) Xeon(R) CPU X5570 @ 2.93GHz stepping : 5 cpu MHz : 1600.000 >uname -a Linux xxxxxx 2.6.24.7-108.el5rt #1 SMP PREEMPT RT Mon Mar 23 10:58:10 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux 

Do you know other ways or libraries that atomic variables can implement for my architecture or if I am doing something wrong (maybe some flag compilations that I should check)?

NOTE. I found stdatomic.h , but only for C ++, unfortunately

Usage example:

 int i =0; i = _sync_fetch_and_add (&i,2); 
+4
source share
2 answers

This answer will become relevant in 2018 .:-)

The next C1X standard is the introduction of atomics as a function of C. See the draft draft of C1X .

+4
source

Try the -march command-line option; atomic inline functions are not available for all target architectures. see also

+2
source

Source: https://habr.com/ru/post/1344787/


All Articles