这里有两个函数:
std::deque<int> q;
// Push lots of elements to q
xxxxx
void foo() {
auto begin = q.cbegin();
auto end = q.cend();
q.erase(begin, end);
}
void bar(int x) { q.push_back(x); }
调用是线程安全的 foo
和bar
来自两个不同的线程?行为是否未定义?
最佳答案
The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. Any such data race results in undefined behavior.
erase
和 push_back
都不是原子的,因此您将遇到数据竞争。
“Effective C++ Digital Collection”告诉我们,您对实现的期望是:
关于c++ - 从两个线程调用 std::deque 上的删除和push_back是否是线程安全的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605780/