C ++ 11 std :: vector in concurrent enviroment [英] C++11 std::vector in concurrent enviroment

查看:126
本文介绍了C ++ 11 std :: vector in concurrent enviroment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题(segfault)在C ++ 11中运行多线程代码。这里是代码:

  #include< vector> 
#include< thread>

std :: vector< int>值;
int i;

void values_push_back()
{
values.push_back(i);
}

int main()
{
while(true)
{
std :: vector< std :: thread>线程;

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



这有什么问题?

$



多线程正在执行 > vector :: push_back()> >向量:: push_back() code>不是线程安全的。对向量的修改需要同步。



A std :: mutex 可以用于同步调用 push_back )

  std :: vector< int&值; 
std :: mutex values_mutex;

void values_push_back()
{
values_mutex.lock();
values.push_back(i);
values_mutex.unlock();
}

此外,变量 i 在没有同步的线程之间共享,这将导致竞争条件(可能的结果是重复 int 添加到向量)。考虑将 int 值作为参数传递给线程,以避免这种情况:

  std :: vector< int>值; 
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() throws(在这种情况下只能是 bad_alloc()但是如果向量更改以保存更复杂的具有抛出构造函数的对象,它变得更重要):

  void values_push_back(int i)
{
std :: lock_guard< std :: mutex> lk(values_mutex);
values.push_back(i);
}


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;
}

And here the backtrace on gdb: http://pastebin.com/5b5TN70c

What's wrong in that?

解决方案

This is unrelated to moving.

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.

A std::mutex could be used to synchronize the calls to push_back():

std::vector<int> values;
std::mutex values_mutex;

void values_push_back()
{
    values_mutex.lock();
    values.push_back(i);
    values_mutex.unlock();
}

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 ints 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();


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);
}

这篇关于C ++ 11 std :: vector in concurrent enviroment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆