Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14859

atomic variables in C

I would like to use atomic variables in C.

I tried the following suggested builtin functions in gcc but I received a link 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 am assuming that my architecture doesn't support them..I thought that probably because it is not INTEL but looking a the CPU info I discovered that I have Intel CPU.

  >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 might implement atomic variables for my architecture or if am I doing something wrong( maybe some compilation flags that I hould check out )?

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

Usage example:

int i =0;
i = _sync_fetch_and_add (&i,2);

Upvotes: 4

Views: 4165

Answers (2)

horsh
horsh

Reputation: 2779

This answer is going to become relevant in the year 201X. :-)

The upcoming C1X standard is to introduce atomics as a C language feature. See the draft C1X standard.

Upvotes: 4

stacker
stacker

Reputation: 68942

Try the -march command line option, the atomic builtins are not available for all target architectures. See also

Upvotes: 2

Related Questions