Using std::ref, content does not change after exiting function

64 Views Asked by At

I have been implementing Strassen algorithm with threads. I have passed data to threads by structures and launching them by function pthread_create() . The problem is I'm operating on std::vector < std::vector<int> > and I can't pass this to structure by reference. I have done some research and decided to use std::ref to wrap content and send it to function. The problem begins after exiting the function; r1 value does not change (inside changes), so there is something incorrect.

Example code:

typedef struct threadData
{
    int n;
    std::vector< std::vector<int> > mat1;
    std::vector< std::vector<int> > mat2;
    std::vector< std::vector<int> > result;
}thread_Data;



/* function adding 2 matrixes */

void *add_Thread(void *arg)
{
    thread_Data *data = (threadData*)arg;
    for (int i = 0; i < data->n; ++i)
    {
        for (int j = 0; j < data->n; ++j)
        {
            data->result[i][j] = data->mat1[i][j] + data->mat2[i][j];
        }
    }

    pthread_exit(0);
}

/* ... code ... */

thread_Data adds;
adds = thread_Data{newSize, a11, a22, std::ref(r1)};  // here passing by std::ref

pthread_t adder;

pthread_create(&adder, NULL, add_Thread, &adds[i]);

pthread_join(adder, NULL);

/* and r1 value does not change here */

How to fix it?

NOTE: *add_Thread() works fine; I use array in my program. The code here is only to show idea.

0

There are 0 best solutions below