title: Using std::atomic from C
body: <p>I have a C library that defined a bunch of platform specific macros for atomic operations. How can I use <code>std::atomic</code> as the implementation for this?</p>
  
  <p>For example the C code has:</p>
  
  <pre><code>#define mylib_atomic_int_add(_pi, _val) do_atomic_int_add(_pi, _val)
  int number = 0;
  mylib_atomic_int_add(&amp;number, 7);
  </code></pre>
  
  <p>And the C++ platform abstraction layer (i.e a static library compiled as C++11 that is linked with the C code) has:</p>
  
  <pre><code>extern "C"
  {
      int do_atomic_int_add(volatile int* i, volatile int v)
      {
          return *i + v;
      } 
  }
  </code></pre>
  
  <p>Obviously this isn't safe because it isn't atomic, since the int is declared in the C code <code>std::atomic</code> can't be used there, but is there some way to use it within <code>do_atomic_int_add()</code>? like <code>return std::atomic&lt;int&gt;::add(*i, v);</code>?</p>
  
  <p>Edit: </p>
  
  <p>I don't know why no one understands what I'm trying to do. Its basically how do I atomically increment an int in C++ which has been passed over from a C function.</p>