【问题标题】:pthread_join crashes with core dumped segfaultpthread_join 因核心转储段错误而崩溃
【发布时间】:2012-12-14 20:13:51
【问题描述】:

我有以下代码在pthread_join 调用时导致段错误。对于很多行我很抱歉,但每个字符都可能很重要。如果它很重要,相同的代码在 WinAPI 下运行良好且没有任何错误。

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

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

在许多代码的末尾,我的struct

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

工作选项是:

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

【问题讨论】:

    标签: c++ pthreads segmentation-fault pthread-join


    【解决方案1】:

    您正在加入自己,因为最后一个线程没有启动,而是使用创建其他线程的线程。检查 pthread_join 的错误码...

    【讨论】:

    • 好的,你的数组是threads = new pthread_t[amount_of_threads - 1],但是当你尝试等待自己时,你正在使用一个索引(这不会起作用,在这种情况下你甚至不等待自己而是访问内存你没有分配,这会产生意想不到的结果)
    • 最后一个没有启动,因为if (i == amount_of_threads - 1) {MyThreadProc(&amp;data[i]);}使用当前线程而不是启动另一个作为最后一个线程。然后,您越界访问数组,因为有效索引来自 0..amount_of_threads-2
    • 我已将代码更改为 new pthread_t[amount_of_threads],删除了 if (i == amount_of_threads - 1) 并尝试仅调用 1 个线程,但出现同样的错误。我错在哪里?
    • 好的。现在您将for(int i = 0; i &lt; amount_of_threads; ++i) pthread_join(threads[i], 0); 更改为for(int i = 0; i &lt;amount_of_threads-1; ++i) pthread_join(threads[i], 0);
    • 非常感谢!当我编译最后一个时 - 没关系。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2017-09-16
    • 2017-08-27
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2020-10-27
    • 2017-09-28
    相关资源
    最近更新 更多