C++11 std::vector in concurrent environment
Asked Answered
A

1

13

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?

Ambrose answered 13/2, 2013 at 17:27 Comment(1)
Please see my comment on hmjds answer and don't blindly copy his code.Aspinwall
F
21

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);
}
Fodder answered 13/2, 2013 at 17:29 Comment(3)
I had a more complex issue I can't reproduce with a simple code. Sorry about that.Ambrose
Your code is not exception-safe. If push_back throws, you will deadlock, use std::lock_guard instead.Aspinwall
also consider #include <boost/lockfree/queue.hpp> AND boost::lockfree::queue <int> values {100};Smutch

© 2022 - 2024 — McMap. All rights reserved.