As known there are Transactional Memory TS (ISO/IEC TS 19841:2015) in the Experimental C++: Is it enough to declare a function as transaction_safe, so they can be used thread-safe?
And operator[]
declared as transaction_safe
only for containers: std::vector
, std::unordered_map
, std::unordered_multimap
, std::unordered_set
, std::unordered_multiset
, std::deque
- Taken from n4514: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4514.pdf
23.4 Associative containers [associative]
23.4.4 Class template map [map]
23.4.4.1 Class template map overview [map.overview]
In 23.4.4.1 [map.overview], add "transaction_safe" to the declarations of all variants of the begin and end member functions and to the declarations of size, max_size, and empty.
But why is not there operator[]
declared as transaction_safe
for std::map
and std::set
(but there are for unordered_map
/unordered_set
)?
And why there are add "transaction_safe" to the declarations of all variants of the begin
and end
member functions for std::map
and std::set
?
Iterators begin
and end
are very necessary for the std::array
, std::vector
or std::list
, but not for an associative array. In an associative array required find or find-and-modify functions: find
, at
, insert
, erase
and operator[]
. Without them, it does not make sense.
Why are ordered std::map and ordered std::set not invited to make transaction_safe?
That unordered_meow::operator[]
is specified to be unconditionally transaction-safe is a defect.
unordered_set
, unordered_multiset
and unordered_multimap
don't even have an operator[]
in the first place.unordered_map::operator[]
must call Hash
and Pred
, and possibly allocate memory and construct a new key-value pair; none of these are necessarily transaction safe.Instead, map::operator[]
's transaction safety is governed by the addition to [container.requirements.general]:
Unless unconditionally specified to be transaction-safe, a function in this Clause is transaction-safe if all required operations are transaction-safe. [ Note: This includes operations on the element type, on
std::allocator_traits
, and onCompare
,Pred
, orHash
objects, depending on the respective function. -- end note ]