并发环境中的 C++11 std::vector
我在 C++11 中运行多线程代码时遇到问题(段错误).这是代码:
I had an issue (segfault) running a multithreaded code in C++11. Here it is the code:
#include <vector>
#include <thread>
std::vector<int> values;
int i;
void values_push_back()
{
values.push_back(i);
}
int main()
{
while(true)
{
std::vector<std::thread> threads;
for(i=0; i<10; ++i)
{
std::thread t(values_push_back);
threads.push_back(std::move(t));
}
for(i=0; i<10; ++i)
threads[i].join();
}
return 0;
}
这里是 gdb 的回溯:http://pastebin.com/5b5TN70c
And here the backtrace on gdb: http://pastebin.com/5b5TN70c
这有什么问题?
推荐答案
这和搬家无关.
多个线程在同一个 vector
上执行 vector::push_back()
但是vector::push_back()
不是线程安全的.vector
的修改需要同步.
Multiple threads are executing vector::push_back()
on the same vector
but
vector::push_back()
is not threadsafe. The modifications to the vector
need to be synchronized.
std::mutex
可能是用于同步对 push_back()
的调用:
std::vector<int> values;
std::mutex values_mutex;
void values_push_back()
{
values_mutex.lock();
values.push_back(i);
values_mutex.unlock();
}
此外,变量 i
在线程之间共享而没有同步,这将导致竞争条件(这可能的结果是重复的 int
添加到矢量
).考虑将 int
值作为参数传递给线程以避免这种情况:
Also, the variable i
is being shared among threads without synchronization which is will result in a race condition (a possible outcome of this is duplicate int
s added to the vector
). Consider passing the int
value as an argument to the thread to avoid this:
std::vector<int> values;
std::mutex values_mutex;
void values_push_back(int i)
{
values_mutex.lock();
values.push_back(i);
values_mutex.unlock();
}
for (int i = 0; i < 10; ++i)
{
threads.push_back(std::thread(values_push_back, i));
}
for (auto& t: threads) t.join();
<小时>
正如 bamboon 所评论的,更喜欢 std::lock_guard
以确保如果 push_back()
抛出(在这种情况下可能只是 bad_alloc()
但如果 vector
更改以容纳具有抛出构造函数的更复杂的对象,则变得更加重要):
As commented by bamboon prefer std::lock_guard
to ensure the lock is released if push_back()
throws (which in this case could only be bad_alloc()
but if the vector
changes to hold more complex objects that have throwing constructors it becomes more important):
void values_push_back(int i)
{
std::lock_guard<std::mutex> lk(values_mutex);
values.push_back(i);
}
相关文章