Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing atomic<T>::store

I'm attempting to implement the atomic library from the C++0x draft. Specifically, I'm implementing §29.6/8, the store method:

template <typename T>
void atomic<T>::store(T pDesired, memory_order pOrder = memory_order_seq_cst);

The requirement states:

The order argument shall not be memory_order_consume, memory_order_acquire, nor memory_order_acq_rel.

I'm not sure what to do if it is one of these. Should I do nothing, throw an exception, get undefined behavior, or do something else?

P.S.: "C++0X" looks kinda like a dead fish :3

like image 365
Joel Barba Avatar asked Sep 17 '10 02:09

Joel Barba


People also ask

How do you declare an atomic variable in C++?

In order to solve this problem, C++ offers atomic variables that are thread-safe. The atomic type is implemented using mutex locks. If one thread acquires the mutex lock, then no other thread can acquire it until it is released by that particular thread.

What is atomic load and store?

Loads and Stores For that to be possible, such data must exist in shared memory or cache. Thus, an atomic load loads data from shared memory to either a register or thread-specific memory, depending on the processor architecture. Atomic stores move data into shared memory atomically.

How do you initialize atomic bool?

However, you can use direct-initialization: std::atomic_bool World::mStopEvent(false); At your wish, you may choose to use braces instead of parentheses: std::atomic_bool World::mStopEvent{false};

What does STD atomic load do?

std::atomic<T>::load Atomically loads and returns the current value of the atomic variable. Memory is affected according to the value of order .


1 Answers

Do what you want. It doesn't matter.

When ISO state that you "shall not do something", doing it is undefined behaviour. If a user does that, they have violated the contract with the implementation, and the implementation is within its rights to do as it pleases.

What you decide to do is entirely up to you. I would opt for whatever makes your implementation "better" (in your eyes, be that faster, more readable, subject to the principle of least astonishment, and so forth).

Myself, I'd go for readability (since I would have to maintain the thing) with speed taking a close second.

like image 133
paxdiablo Avatar answered Sep 20 '22 00:09

paxdiablo