title: How to perform atomic operations on Linux that work on x86, arm, GCC and icc?
body: <p>Every Modern OS provides today some atomic operations: </p>
  
  <ul>
  <li>Windows has <code>Interlocked*</code> API</li>
  <li>FreeBSD has <code>&lt;machine/atomic.h&gt;</code></li>
  <li>Solaris has <code>&lt;atomic.h&gt;</code></li>
  <li>Mac OS X has <code>&lt;libkern/OSAtomic.h&gt;</code></li>
  </ul>
  
  <p>Anything like that for Linux?</p>
  
  <ul>
  <li>I need it to work on most Linux supported platforms including: x86, x86_64 and  <strong>arm</strong>.</li>
  <li>I need it to work on at least GCC and Intel Compiler.</li>
  <li>I need not to use 3rd par library like glib or qt.</li>
  <li>I need it to work in C++ (C not required)</li>
  </ul>
  
  <p>Issues:</p>
  
  <ul>
  <li>GCC atomic builtins <code>__sync_*</code> are not supported on all platforms (ARM) and are not supported by the Intel compiler.</li>
  <li>AFAIK <code>&lt;asm/atomic.h&gt;</code> should not be used in user space and I haven't successfully used it at all. Also, I'm not sure if it would work with Intel compiler.</li>
  </ul>
  
  <p><strong>Any suggestions?</strong></p>
  
  <p>I know that there are many related questions but some of them point to <code>__sync*</code> which is not feasible for me (ARM) and some point to <code>asm/atomic.h</code>.</p>
  
  <p>Maybe there is an inline assembly library that does this for GCC (ICC supports gcc assembly)?</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>There is a very partial solution for add operations only (allows implementing atomic counter but not lock free-structures that require CAS):</p>
  
  <p>If you use <code>libstc++</code> (Intel Compiler uses <code>libstdc++</code>) then you can use <code>__gnu_cxx::__exchange_and_add</code> that defined in <code>&lt;ext/atomicity.h&gt;</code> or <code>&lt;bits/atomicity.h&gt;</code>. Depends on compiler version.</p>
  
  <p>However I'd still like to see something that supports CAS.</p>