C ++ atom with non-trivial type?

Reading documents on boost::atomic and std::atomic leaves me confused as to whether atomic support non-trivial types?

That is, if a type (value-) is specified, which can only be written / read by enabling read / write in the full mutex, since it has a non-trivial copy-ctor / assign operator, it is assumed that this is supported by std::atomic (since boost clearly indicates that it is UB).

Should I provide specialization docs talking about myself for non-trivial types?


Note. I clicked on this because I have a cross-thread callback object boost::function<bool (void)> simpleFn; which should be installed / reset atomically. Having a separate fragment of the mutex / critical section, or even wrapping both an atomically similar helper type with a simple set, and getting seemingly easy enough, but is there anything out of the box?

+5
source share
2 answers

Arne's answer already indicates that the standard requires trivially copied types for std::atomic .

Here are some considerations why atomics may not be the best tool for your problem in the first place: Atomics are the fundamental building primitives for creating thread-safe data structures in C ++. They must be the lowest-level building blocks to build more powerful data structures, such as thread-safe containers.

In particular, atomistics is usually used to construct data structures without blocking. To lock data structures, primitives such as std::mutex and std::condition_variable are better combined, if only because it is very difficult to write a lock code with an automatics without introducing a lot of lively waiting.

Therefore, when you think of std::atomic , the first association should be blocked (despite the fact that most atomic types are technically allowed to have blocking implementations). What you are describing is a simple parallel data structure based on locking, so wrapping it with an atom should already feel wrong from a conceptual point of view.

Unfortunately, it is currently unclear how to express in language that the data structure is thread safe (which, I think, was your main intention to use atomic in the first place). Herb Sutter has some interesting ideas on this subject, but in my opinion, now we just have to admit the fact that we have to rely on the documentation to tell how certain data structures behave with respect to thread safety.

+3
source

The standard states (Β§29.5.1) that

T argument type must be trivially copied

Set to no, you cannot use types with non-trivial copy-ctor or assignment-op.

However, like any template in the std namespace, you can specialize a template for any type for which it was not intended to be implemented. Therefore, if you really want to use std::atomic<MyNonTriviallyCopyableType> , you need to provide the specialization yourself. The way this specialization behaves depends on you, that is, you are free to knock down the foot or foot of anyone who uses this specialization, because it is simply outside the scope of the standard.

+3
source

Source: https://habr.com/ru/post/947540/


All Articles