0

I have the following code that causes segfault at pthread_join calls. I'm sorry for many lines, but every char can be important. If it's important, the same code works under WinAPI good and without any errors.

void solution_of_matrix(int amount_of_threads,  vector< vector<double> > &matrix)
{
    pthread_t * threads = new pthread_t[amount_of_threads - 1];
    matrix_struct * data = new matrix_struct[amount_of_threads];
    /* ... */
    for (size_t i = 0; i < amount_of_threads; ++i)
    {
        /* ... */
        if (i == amount_of_threads - 1)
        {
            MyThreadProc(&data[i]);
        }
        else
        {   
            pthread_create(&threads[i], 0, MyThreadProc, &data[i]);
        }
    }
    for(int i = 0; i < amount_of_threads; ++i)
        pthread_join(threads[i], 0); // <- ERROR
    delete[] threads;
    delete[] data;
}

And MyThreadProc:

void* MyThreadProc(void* lpParam)
{   
    matrix_struct * data = (matrix_struct*)lpParam;
    /*... */

    pthread_mutex_lock(&mutex);
    for (i = (data->i); i < (*data->matrix).size(); i++)
    {       
    if ((*data->matrix)[i][i] == 0 )
        for (j = (data->i + 1); j <(*data->matrix).size(); j++)
            if ((*data->matrix)[j][i] != 0 )
            {
             vector< double > v;
             for(size_t k = 0; k < (*data->matrix)[j].size(); ++k)
                 v.push_back((*data->matrix)[j][k]);

             for( int q = 0; q <  (*data->matrix)[j].size(); q++)
                (*data->matrix)[j][q] = (*data->matrix)[i][q];

             for ( int w = 0; w < (*data->matrix)[i].size(); w++)
                (*data->matrix)[i][w] = v[w];
             break;
            }   
    }

    for (i = (data->i); i < data->i + data->sz ; i++)
    {   
        if ( i !=(*data->matrix).size() - 1)
        {
            tmp = (*data->matrix)[i][i];
            for (j = n; j >= i; j--)    
                    (*data->matrix)[i][j] /= tmp;

            for (j = i+1; j < n; j++)
            {               
                tmp = (*data->matrix)[j][i];
                for (k = n; k >= i; k--)
                    (*data->matrix)[j][k] -= tmp*(*data->matrix)[i][k];         
            }
        }
    }

    pthread_mutex_unlock(&mutex);
    return 0;
}

And at the end of many code, my struct:

struct matrix_struct
{
    vector< vector<double> > * matrix;
    size_t i, sz;
};

Th working option is:

void solution_of_matrix(int amount_of_threads,  vector< vector<double> > &matrix)
{
    pthread_t * threads = new pthread_t[amount_of_threads];
    matrix_struct * data = new matrix_struct[amount_of_threads];
    /* ... */
    for (size_t i = 0; i < amount_of_threads; ++i)
    {
        /* ... */
        pthread_create(&threads[i], 0, MyThreadProc, &data[i]); //just simplifyed
    }
    for(int i = 0; i < amount_of_threads; ++i)
        pthread_join(threads[i], 0);
    delete[] threads;
    delete[] data;
}
zerospiel
  • 632
  • 8
  • 20

1 Answers1

1

You are joining with yourself because the last thread is not started but using the thread which creates the other ones. Check the error-code of pthread_join...

pbhd
  • 4,384
  • 1
  • 20
  • 26
  • ok, and your array is `threads = new pthread_t[amount_of_threads - 1]`, but you are using one index ahead when you attempt to wait for youself (which wont work, and in this case you even dont wait for yourself but access memory which you did not allocate, which will give unexpected results) – pbhd Dec 14 '12 at 20:23
  • The last one is not started, because that `if (i == amount_of_threads - 1) {MyThreadProc(&data[i]);}` uses the current thread instead of starting another one as the last thread. And later, you access your array out of bounds, because valid indices are from 0..amount_of_threads-2 – pbhd Dec 14 '12 at 20:27
  • i've changed the code to `new pthread_t[amount_of_threads]`, deleted the `if (i == amount_of_threads - 1)` and trying to call just 1 thread, there is the same error. Where is my wrong line? – zerospiel Dec 14 '12 at 20:28
  • Ok. Now you change `for(int i = 0; i < amount_of_threads; ++i) pthread_join(threads[i], 0);` to `for(int i = 0; i – pbhd Dec 14 '12 at 20:30
  • Thank u very much! When i compiled the last one — it's okay. – zerospiel Dec 14 '12 at 20:30