- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
来自 emplace_back()
的文档摘录:
- Iterator validity
All iterators related to this container are invalidated, but pointers and references remain valid, referring to the same elements they were referring to before the call.
- Data races
The container is modified.
No contained elements are accessed by the call: concurrently accessing or modifying them is safe (although see iterator validity above).
以及来自 operator[]()
的文档摘录:
- Data races
The container is accessed (neither the const nor the non-const versions modify the container).
Element n is potentially accessed or modified. Concurrently accessing or modifying other elements is safe.
因此,鉴于 deque 的某个实例至少有一个元素,通过 operator[]()
访问它并调用 emplace_back()
在容器上并发确实是线程安全的?
我倾向于说是,但无法确定 emplace_back()
文档中的“访问”是否包括使用 operator[]()
如:
int access( std::deque< int > & q )
{
return q[ 0 ];
}
void emplace( std::deque< int > & q , int i )
{
q.emplace_back( i );
}
同时调用两个函数,或者“访问”仅适用于已采用某些引用或指针的元素:
std::deque< int > q { 1 };
auto * ptr = & q[ 0 ]
std::thread t1 ( [ ptr ]{ * ref = 0; } );
std::thread t2 ( [ & q ]{ q.emplace_back( 2 ); } );
编辑:为了进一步引用,以下是 C++ 14 标准(实际上是 November 2014 Working Draft, N4296)关于在 deque
中插入关于引用和迭代器有效性的说明:
- 23.3.3.4 deque modifiers
(...)
- Effects: An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque. An insertion at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.
(...)
最佳答案
在标准类的对象上同时调用任何两个方法都是不安全的,除非两者都是 const
,或者除非另有说明(例如 std::mutex::锁定()
)。对此进行了更详细的探讨 here
因此,同时使用 emplace_back
和 operator[]
是不安全的。但是,由于引用/指针有效性,您可以安全地使用先前获得的对 deque
元素的引用,同时调用 emplace_back
/push_back
您引用的规则,例如:
int main()
{
std::deque<int> d;
d.push_back(5);
auto &first = d[0];
auto task = std::async(std::launch::async, [&] { first=3; });
d.push_back(7);
task.wait();
for ( auto i : d )
std::cout << i << '\n';
}
这将安全地输出 3 和 7。请注意,引用 first
是在启动异步任务之前创建的。
关于c++ - 从 std::deque 线程安全地同时调用 emplace_back() 和 operator[]() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001062/
这个问题在这里已经有了答案: Is using std::deque or std::priority_queue thread-safe? [duplicate] (3 个答案) Thread s
我是一名学生,我的操作系统类(class)项目有一点问题,这对于作业规范本身来说有点多余: 虽然我可以将 100 万个双端队列推送到我的双端队列中,但我无法推送约 1000 万或更多。 现在,在实际的
std::deque 在 CppReference 中有很好的记录,但是 boost::deque 的 documentation看起来和标准的一样,只是增加了一些方法,比如nth和index_of。
Scala 是否具有类似于 Java Deque 或 Python deque 的双面队列? 我在 Scala 2.12 API 中只看到 Stack 和 Queue 但只想仔细检查一下。 最佳答案
好的,我正在实现一个与 Apple 的日历应用程序非常相似的日历。我的 UICollectionView 中有多种类型的单元格。我有垂直线单元格、水平线单元格、现在线单元格和事件单元格。 每当我滚动时
目前在我的项目中我有两个静态方法PushObjects 和ProcessObject。 PushObject 方法将数据推回静态双端队列,此方法可由多个线程访问,但 ProcessObject 始终由
使用 http://www.cppreference.com/wiki/stl/deque/insert作为引用,我在某些位置将值插入双端队列。 例如,如果双端队列 A 是: a, b, d, e,
Python 的 collections.deque有一个 maxlen 参数,这样 [...] the deque is bounded to the specified maximum lengt
这是我的第一个问题,希望大家都好。我必须编写双端队列或双端队列的数组实现,但在理解前面方法的入队元素时遇到了一些麻烦,我通过摆弄了一下让它工作,但我仍然很难理解逻辑: void addAtFront(
我试图解决 Java Deque 上的 HackerRank 问题。除了具有 100,000 个输入的案例之外,我的代码通过了所有案例。 问题:在这个问题中,给你 N 个整数。您需要在大小为 M 的所
我正在编写 Deque 以便能够从 Front 和 Rear 中添加和删除....我认为我的逻辑是错误的,但我无法弄清楚!因为当我从前面插入时,它必须从后面移除,但也从前面移除。 你会检查我的代码并帮
我在使用 deque 容器时有些困惑。 我将 vector 与 deque 进行了比较,我动态地输入了 Integer 值并观察到在几次插入之后 vector 的对象开始四处移动并且地址已被更改,这似
由于我对内存有严格的要求,因此在从双端队列的开头删除一个范围后,我尝试使用deque::shrink_to_fit。但是,它不起作用,我只看到libstdc++使用带有副本的swap技巧实现了shri
我需要为继承到 deque 的类添加功能,但更喜欢看到 collections.deque 中的代码来实现一个新类 final。 >>> from _collections import deque,
本文实例讲述了python3 deque 双向队列创建与使用方法。分享给大家供大家参考,具体如下: 创建双向队列 ?
考虑以下 C++ 程序: #include #include using namespace std; int main() { deque d(30000000); cout
尝试使用 Deque 数据结构来回答一个编程问题,以查找乘积小于目标的所有子数组。 如上所述,我想使用 Deque 数据结构。我查看了用法,认为我做对了,但是使用了 const Deque = req
双端队列实现我实现了一个通用的 Deque 数据结构。 请检查此实现 这个错误对我来说没有意义,请告诉我一点信息 import java.util.NoSuchElementException; im
我需要编写自己的 Deque 类,并且必须使用双向链表实现来存储数据。问题是编写方法pushfromLeft(Thing thing),它将插入到双端队列的左侧。以下是我迄今为止所拥有的,但似乎不起作
标准说: A deque is a sequence container that supports random access iterators (27.2.7). In addition, it
我是一名优秀的程序员,十分优秀!