The following is a producer/consumer example.
atomic in, out;
while ( (in + 1) %capacity== out);while ( in == out ) ;
/*do something*/ /* do something */
in = (in + 1) % capacity; out = (out + 1) % capacity;
As you can see, in (out) only modified by P1 (P2).
I am wondering if there is any possible benefit in which we replace in (out) with an ordinary variable. So that the above code become,
const size_t local_in = (in + 1) % capacity;const size_t local_out = out;
while (local_in == out); while ( in ==local_out ) ;
/* do something */ /* do something */
in = local_in; out = (local_out + 1) % capacity;
Any comment will be appreciate.