Multi-threading does not work correctly using std::thread (C++ 11)

Posted by user1364743 on Stack Overflow See other posts from Stack Overflow or by user1364743
Published on 2013-07-02T16:53:30Z Indexed on 2013/07/02 17:05 UTC
Read the original article Hit count: 128

Filed under:
|
|

I coded a small c++ program to try to understand how multi-threading works using std::thread. Here's the step of my program execution :

  1. Initialization of a 5x5 matrix of integers with a unique value '42' contained in the class 'Toto' (initialized in the main).
  2. I print the initialized 5x5 matrix.
  3. Declaration of std::vector of 5 threads.
  4. I attach all threads respectively with their task (threadTask method). Each thread will manipulate a std::vector<int> instance.
  5. I join all threads.
  6. I print the new state of my 5x5 matrix.

Here's the output :

42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42

42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42

It should be :

42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42

0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4

Here's the code sample :

#include <iostream>
#include <vector>
#include <thread>

class       Toto
{
public:
    /*
    ** Initialize a 5x5 matrix with the 42 value.
    */
    void    initData(void)
    {
        for (int y = 0; y < 5; y++) {
            std::vector<int> vec;

            for (int x = 0; x < 5; x++) {
                vec.push_back(42);
            }
            this->m_data.push_back(vec);
        }
    }

    /*
    ** Display the whole matrix.
    */
    void    printData(void) const
    {
        for (int y = 0; y < 5; y++) {
            for (int x = 0; x < 5; x++) {
                printf("%d ", this->m_data[y][x]);
            }
            printf("\n");
        }
        printf("\n");
    }

    /*
    ** Function attached to the thread (thread task).
    ** Replace the original '42' value by another one.
    */
    void    threadTask(std::vector<int> &list, int value)
    {
        for (int x = 0; x < 5; x++) {
            list[x] = value;
        }
    }

    /*
    ** Return the m_data instance propertie.
    */
    std::vector<std::vector<int> >  &getData(void)
    {
        return (this->m_data);
    }

    private:
        std::vector<std::vector<int> > m_data;
};

int         main(void)
{
    Toto    toto;

    toto.initData();

    toto.printData(); //Display the original 5x5 matrix (first display).

    std::vector<std::thread> threadList(5); //Initialization of vector of 5 threads.

    for (int i = 0; i < 5; i++) {  //Threads initializationss

        std::vector<int> vec = toto.getData()[i]; //Get each sub-vectors.
        threadList.at(i) = std::thread(&Toto::threadTask, toto, vec, i); //Each thread will be attached to a specific vector.
    }

    for (int j = 0; j < 5; j++) {
        threadList.at(j).join();
    }

    toto.printData(); //Second display.
    getchar();

    return (0);
}

However, in the method threadTask, if I print the variable list[x], the output is correct. I think I can't print the correct data in the main because the printData() call is in the main thread and the display in the threadTask function is correct because the method is executed in its own thread (not the main one). It's strange, it means that all threads created in a parent processes can't modified the data in this parent processes ? I think I forget something in my code. I'm really lost. Does anyone can help me, please ? Thank a lot in advance for your help.

© Stack Overflow or respective owner

Related posts about c++

Related posts about multithreading