【问题标题】:Why are ordered std::map and ordered std::set not invited to make transaction_safe?为什么没有邀请订购 std::map 和订购 std::set 来制作 transaction_safe?
【发布时间】:2016-12-09 09:17:47
【问题描述】:

众所周知,实验 C++ 中有事务性内存 TS (ISO/IEC TS 19841:2015):Is it enough to declare a function as transaction_safe, so they can be used thread-safe?

并且 operator[] 声明为 transaction_safe 仅适用于容器:std::vectorstd::unordered_mapstd::unordered_multimapstd::unordered_setstd::unordered_multisetstd::deque - 取自 n4514:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4514.pdf

23.4 关联容器 [associative]

23.4.4 类模板图[map]

23.4.4.1 类模板地图概述[map.overview]

在 23.4.4.1 [map.overview] 中,将“transaction_safe”添加到声明中 begin 和 end 成员函数的所有变体以及 size、max_size 和 empty 的声明。

但是为什么operator[] 没有为std::mapstd::set 声明为transaction_safe(但有unordered_map/unordered_set)?

为什么要在std::mapstd::setbeginend 成员函数的所有变体的声明中添加“transaction_safe”?

迭代器 beginend 对于 std::arraystd::vectorstd::list 是非常必要的,但对于关联数组则不是。在关联数组中需要查找或查找并修改函数:findatinserteraseoperator[]。没有它们,就没有意义。

为什么命令 std::map 和命令 std::set 不被邀请来使事务安全?

【问题讨论】:

  • 我强烈怀疑operator[]unordered_meow 的无条件事务安全是一个缺陷。

标签: c++ concurrency stl c++14 transactional-memory


【解决方案1】:

unordered_meow::operator[] 被指定为无条件事务安全是一个缺陷。

  • 首先,unordered_setunordered_multisetunordered_multimap 甚至都没有 operator[]
  • unordered_map::operator[]必须调用HashPred,并可能分配内存并构造新的键值对;这些都不一定是交易安全的。

相反,map::operator[] 的事务安全性由添加到 [container.requirements.general]:

除非无条件指定为事务安全,否则 如果所有必需的操作都满足,则此子句是事务安全的 交易安全。 [注意:这包括对元素的操作 在std::allocator_traitsComparePredHash 对象上键入, 取决于各自的功能。 -- 尾注]

【讨论】:

    猜你喜欢
    • 2014-03-15
    • 1970-01-01
    • 2011-08-03
    • 2014-04-14
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多