C How to order shm writes

Some data is written into shared memory file, mapped with MAP_NOSYNC | MAP_SHARED. Then an index is updated in that same memory area. Is the following a/the proper way to force this order, index last ?

Code:
#include <machine/atomic.h>
...
mb();

Juha

Following seems to do the same thing:
Code:
struct {
  _Atomic int index;
  int data[2];
} *shm;

void
update(int value)
{
  int i = shm->index + 1;

  shm->data[i & 1] = value;

  shm->index += 0; /* needed or not ? */
  shm->index++;
}
 
Last edited:
Back
Top