mutex - C++ atomic with non-trivial type? -


reading docs on boost::atomic , on std::atomic leaves me confused whether atomic interface supposed support non-trivial types?

that is, given (value-)type can written/read enclosing read/write in full mutex, because has non-trivial copy-ctor/assignment operator, supposed supported std::atomic (as boost states ub).

am supposed provide specialization docs talk myself non-trivial types?


note: hitting on because have cross-thread callback object boost::function<bool (void)> simplefn; needs set/reset atomically. having separate mutex / critical section or wrapping both in atomic-like helper type simple set , seem easy enough, there out of box?

arne's answer points out standard requires trivially copyable types std::atomic.

here's rationale why atomics might not right tool problem in first place: atomics fundamental building primitives building thread-safe data structures in c++. supposed lowest-level building blocks constructing more powerful data structures thread-safe containers.

in particular, atomics used building lock-free data structures. locking data structures primitives std::mutex , std::condition_variable way better match, if fact hard write blocking code atomics without introducing lots of busy waiting.

so when think of std::atomic first association should lock-free (despite fact of atomic types technically allowed have blocking implementations). describe simple lock-based concurrent data structure, wrapping in atomic should feel wrong conceptual point of view.

unfortunately, not clear how express in language data structure thread-safe (which guess primary intent using atomic in first place). herb sutter had some interesting ideas on issue, guess have accept fact have rely on documentation communicate how data structures behave regards thread-safety.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -