#pragma omp atomic

Description

The omp atomic directive identifies a specific memory location that must be updated atomically and not be exposed to multiple, simultaneous writing threads.

Syntax

Read syntax diagramSkip visual syntax diagram>>-#--pragma--omp atomic--+-----------+------------------------><
                          '-statement-'

where statement is an expression statement of scalar type that takes one of the forms that follow:

statement Conditions
x bin_op = expr where:
bin_op
is one of:
+  *  -  /  &  ^  |  <<  >>
expr
is an expression of scalar type that does not reference x.
x++  
++x  
x--  
--x  

Notes

Load and store operations are atomic only for object x. Evaluation of expr is not atomic.

All atomic references to a given object in your program must have a compatible type.

Objects that can be updated in parallel and may be subject to race conditions should be protected with the omp atomic directive.

Examples

extern float x[], *p = x, y;
/* Protect against race conditions among multiple updates.  */
#pragma omp atomic
x[index[i]] += y;
/* Protect against races with updates through x.            */
#pragma omp atomic
p[i] -= 1.0f;