0

Consider the following (non)code example:

#include <stdatomic.h>

void setup()
{
  // Nowt.
}

void loop()
{
  // Nowt squared.
}

This generates compile errors for several lines of code in stdatomic.h, e.g.:

error: '_Atomic' does not name a type
 typedef _Atomic _Bool atomic_bool;
         ^~~~~~~

error: 'atomic_flag' does not name a type; did you mean 'atomic_load'?
 } atomic_flag;
   ^~~~~~~~~~~
   atomic_load

I've tried compiling it with the Arduino IDE for Uno. And I've tried godbolt.org with several compilers; some compile Ok; some don't.

Is there a way to get it to work for Uno? Which architectures support it?

What I would like to achieve is an atomic read/write of a struct, something like this:

#include <stdatomic.h>

typedef _Atomic struct
{
  int i;
  bool b;
  float f;
} Data;

void foo()
{

 Data d;// = {3, false, 1.234};
 atomic_init(&d, {3, false, 1.234});  // Some compile this; others don't.

 Data copy;
 copy = d;                // Compiles OK.
 atomic_store(&copy, d);  // Doesn't compile even with compilers that compile _Atomic. Syntax error? See Note 1.
}

Note 1:

error: no viable conversion from 'Data' (aka '_Atomic(struct (anonymous struct at <source>:5:17))') to 'struct (anonymous struct at <source>:5:17)'

 atomic_store(&copy, d);
  • 1
    What are you trying to achieve with atomic operations on a little AVR chip with no OS running on it and no threads? – Majenko Sep 19 '20 at 16:04
  • You should read this thread which basically says "No." unless you hack it around and add extra stuff to just hide it. – Majenko Sep 19 '20 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.