stdatomic

Is it possible to have a release-sequence from a release store operation to a store in a different thread?

假如想象 提交于 2019-12-05 07:18:50
I'm aware that a synchronizes-with relationship will occur between a release store operation in thread 2 and a acquire load operation in thread 1 even if that load operation isn't directly reading the value stored by thread 2, provided that there is a "release sequence" between the release store operation and the store that is actually being read as long as: The store that is actually being read is in the same thread as the release store operation. In modification-order there is no store in other threads between the release store operation and the store that is actually being read (read-modify

Can std::atomic be safely used with OpenMP

独自空忆成欢 提交于 2019-12-05 02:48:43
I'm currently trying to learn ow to use OpenMP and I have a question. Is it safe to do something like that : std::atomic<double> result; #pragma omp parallel for for(...) { result+= //some stuff; } Or shall I use : double result; #pragma omp parallel for for(...) { double tmp=0; //some stuff; #pragma omp atomic result+=tmp; } Thanks ! Edit : I know the most simple way to handle that is using an array, but Im asking because I'm curious Officially, no. In practice, probably. Page Section 1.7 page 32 of the OpenMP 5.0 Specification says: While future versions of the OpenMP specification are

C++ memory model: do seq_cst loads synchronize with seq_cst stores?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:47:11
In the C++ memory model, there is a total order on all loads and stores of all sequentially consistent operations. I'm wondering how this interacts with operations that have other memory orderings that are sequenced before/after sequentially consistent loads. For example, consider two threads: std::atomic<int> a(0); std::atomic<int> b(0); std::atomic<int> c(0); ////////////// // Thread T1 ////////////// // Signal that we've started running. a.store(1, std::memory_order_relaxed); // If T2's store to b occurs before our load below in the total // order on sequentially consistent operations, set

Construct-in-place an unmoveable object in a map

若如初见. 提交于 2019-12-04 03:28:27
问题 I'm trying to construct an object in a map that contains an atomic, so it can neither be copied nor moved AFAICT. My reading of C++ reference is that map emplace should be able to do this. But the following code does not compile because of deleted or non-existent constructors. Using make_pair does not help. #include <atomic> #include <unordered_map> class Z { std::atomic<int> i; }; std::unordered_map<int, Z> map; void test(void) { map.emplace(0, Z()); // error map[0] = Z(); // error } Is this

GCC reordering up across load with `memory_order_seq_cst`. Is this allowed?

≯℡__Kan透↙ 提交于 2019-12-04 02:24:24
Using a simplified version of a basic seqlock , gcc reorders a nonatomic load up across an atomic load(memory_order_seq_cst) when compiling the code with -O3 . This reordering isn't observed when compiling with other optimization levels or when compiling with clang ( even on O3 ). This reordering seems to violate a synchronizes-with relationship that should be established and I'm curious to know why gcc reorders this particular load and if this is even allowed by the standard. Consider this following load function: auto load() { std::size_t copy; std::size_t seq0 = 0, seq1 = 0; do { seq0 = seq

error C2280: attempting to reference a deleted function (atomic<int>)

≯℡__Kan透↙ 提交于 2019-12-03 16:01:42
I have a class A with a member variable _atomicVar of type std::atomic<int> . #include <atomic> class A { public: A(); ~A(); private: std::atomic<int> _atomicVar; }; If I build the project I get the following error: error C2280: 'std::atomic<int>::atomic(const std::atomic<int> &)' : attempting to reference a deleted function I'm mainly a C# developer so I don't know every detail of C++ (yet). I don't know where I use the copy c'tor of atomic<int> . I also tried to initialize _atomicVar : std::atomic<int> _atomicVar { 0 }; ... but that didn't work. I would expect that _atomicVar (without an

Is it necessary to use a std::atomic to signal that a thread has finished execution?

拈花ヽ惹草 提交于 2019-12-03 10:19:52
I would like to check if a std::thread has finished execution. Searching stackoverflow I found the following question which addresses this issue. The accepted answer proposes having the worker thread set a variable right before exiting and having the main thread check this variable. Here is a minimal working example of such a solution: #include <unistd.h> #include <thread> void work( bool* signal_finished ) { sleep( 5 ); *signal_finished = true; } int main() { bool thread_finished = false; std::thread worker(work, &thread_finished); while ( !thread_finished ) { // do some own work until the

“Use of deleted function” error with std::atomic_int

你离开我真会死。 提交于 2019-12-02 21:17:21
I want to use an std::atomic_int variable. In my code, I have: #include <atomic> std::atomic_int stop = 0; int main() { // Do something } And this gives me a compile error: use of deleted function 'std::__atomic_base<_IntTp>::__atomic_base(const std::__atomic_base<_IntTp>&) [with _ITp = int]' std::atomic_int stop = 0; ^ Any idea on what's going on? Your code is attempting to construct a temporary std::atomic_int on the RHS, then use the std::atomic_int copy constructor (which is delete d) to initialise stop , like so: std::atomic_int stop = std::atomic_int(0); That's because copy

Why only std::atomic_flag is guaranteed to be lock-free?

邮差的信 提交于 2019-12-01 18:26:01
From C++ Concurrency in Action: difference between std::atomic and std::atomic_flag is that std::atomic may not be lock-free; the implementation may have to acquire a mutex internally in order to ensure the atomicity of the operations I wonder why. If atomic_flag is guaranteed to be lock-free, why isn't it guaranteed for atomic<bool> as well? Is this because of the member function compare_exchange_weak ? I know that some machines lack a single compare-and-exchange instruction, is that the reason? First of all, you are perfectly allowed to have something like std::atomic<very_nontrivial_large

Construct-in-place an unmoveable object in a map

送分小仙女□ 提交于 2019-12-01 17:52:47
I'm trying to construct an object in a map that contains an atomic, so it can neither be copied nor moved AFAICT. My reading of C++ reference is that map emplace should be able to do this. But the following code does not compile because of deleted or non-existent constructors. Using make_pair does not help. #include <atomic> #include <unordered_map> class Z { std::atomic<int> i; }; std::unordered_map<int, Z> map; void test(void) { map.emplace(0, Z()); // error map[0] = Z(); // error } Is this possible, and if not, why not? EDIT: Compiler is gcc 4.8.1, on Linux map.emplace(std::piecewise