我知道关于同时查找和插入的其他类似问题,我的问题是特定于同时查找和插入相同的键。
根据容器要求23.2.5 "15的c++ 14标准。如果(N+n)
根据这个要求,如果我使用unordered_map的reserve方法来预分配存储桶,这应该会照顾到大多数竞争条件。但是,如果您同时从多个线程中插入并找到相同的键,该怎么办呢?
更新:我真正的意思是同时插入时会发现读垃圾?
发布于 2020-04-06 06:58:50
我花了一些时间阅读了gcc的实现。
如果一个线程使用插入,则返回
unordered_map mp;mpkey =值;
一个线程使用以下命令读取: auto it =mp.find(键);//然后使用它访问键值对
是的,使用由find()返回的迭代器可能会得到垃圾,因为这就是在mpkey = value:期间发生的事情
1. operator[] dynamically create a node containing (empty\_value)
2. bucket pointers point to the newly created (empty\_value) node.
3. the "=" then put "value" in the newly created node.find(key)可以在步骤2之后和步骤3完成之前读取垃圾。
如果我们使用unordered_map::insert,那么它将会不同,因为发生的是:
1. Node containing (value) is dynamically created.
2. bucket pointer points to the newly created node.
因此,当使用find (键)时,要么是unordered_map找不到键-值对,要么是键-值对已经被完全创建。
这是我对实现的理解。如果我错了,请纠正我。
https://stackoverflow.com/questions/60572803
复制相似问题