Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a consume operation in the C++11 Standard?

I have seen that this question on acquire, release, consume, etc exists, however, no answer really defines what a "consume operation" actually is.

In 1.10 paragraph 5 it states:

A synchronization operation on one or more memory locations is either a consume operation, an acquire operation, a release operation, or both an acquire and release operation.

I wondered if someone can explain what this is as it is used in section 1.10 of the C++11 standard?

like image 956
Tony The Lion Avatar asked Mar 07 '13 11:03

Tony The Lion


1 Answers

Apparently, after some searching around, a "consume operation" is a memory operation where a value read from memory is used after the load in several operations, and creates a data dependency.

Being able to order operations without introducing explicit memory fences, is what the apparent (from my understanding) goal is of memory_order_consume.

With memory_order_consume, the compiler and CPU are required to order the load in question against only those subsequent loads and stores whose address or value are computed from the value loaded. source

Later in this text it says:

/*Example code elided */

The key point is that the atomic_load_explicit() using memory_order_consume guarantees that subsequent accesses will see any initialization carried out by insert_foo(), even if they are executing concurrently, and without the overhead of explicit memory-fence instructions. In constrast, memory_order_acquire would require explicit memory barriers on weakly ordered systems and would overconstrain compiler optimizations on all systems.

C++11 Standard draft n3485 defines memory_order_consume as:

29.3 Order and consistency [atomics.order]

memory_order_consume: a load operation performs a consume operation on the affected memory location.

So from my understanding it is to order memory loads so that subsequent loads and stores using that load are indeed subsequent. In other words, the load is being consumed.

like image 87
Tony The Lion Avatar answered Sep 20 '22 20:09

Tony The Lion