C: pthread_create 上的段错误

标签 c pthreads segmentation-fault

我在 GCC 中运行我的程序时遇到了段错误。这是一个相当长的程序,所以我只发布我认为相关的部分;如果需要更多信息,请告诉我。

void *RelaxThread(void *para) {
    printf("Entering RelaxThread...\n");
    struct Parameter *parameters;
    parameters = (struct Parameter *) para;
    /*Some code here*/
    pthread_exit(NULL);
}

int relax(double **matrix, int size, int threads, double precision) {
    keepRunning = 0;
    int rowAllocation = (size-2) / threads;     
    struct Parameter para[threads];
    pthread_t threadcount[threads];
    int current;
    int startRow = 1;
    long t;    
    for(t=0; t<threads; t++){       
        para[t].matrix = matrix;
        para[t].size = size;
        para[t].threads = threads;
        para[t].precision = precision;
        para[t].allocation = rowAllocation;
        para[t].threadId = t;
        para[t].startRow = startRow;
        startRow += rowAllocation;
        printf("DebugMsg1\n");
        current = pthread_create(&threadcount[t], NULL, RelaxThread, (void *) &para[t]);
        printf("DebugMsg2\n");
    }
    void *status;
    int rc;
    for(t=0; t<threads; t++) {
        rc = pthread_join(threadcount[t], &status);
        if (rc) {
            exit(-1);
        }
    }
    if (keepRunning) {
        printf("Iterating...\n");
        relax(matrix, size, threads, precision); 
    }
}

int main(int argc, char *argv[])
{
    int const size = 8;
    int const threads = 2;
    double const precision = 1e-6;
    double **matrix = (double **) malloc(size * sizeof(double));
    populateMatrix(matrix, size);
    relax(matrix, size, threads, precision);
    free(matrix);
    pthread_exit(NULL);
    return 0;
}

当大小设置为 8 或以下时,代码运行正常。然而,任何比这更大的(它现在只支持偶数)都会在 relax() 的第二次迭代中给出一个段错误。因此,调试消息运行为:

Debug1
Debug2
Debug1
Debug2
Entering RelaxThread...
Entering RelaxThread...
Iterating...
Debug1
Debug2
Debug1
Segmentation Fault

事实上,它在大小为 8 或更小的地方运行,但在超过这个数字时崩溃,这让我完全困惑,我花了很长时间试图弄清楚为什么会发生这种情况。我完全承认我的代码既不是最简洁也不是最高效的,但非常感谢您提供有关失败原因的建议。

编辑 1:按要求附加 GDB backlog。

Debug1
[New Thread 0x40040940 (LWP 23270)]
Debug2
Debug1
Entering RelaxThread...
Entering RelaxThread 0
[New Thread 0x40081940 (LWP 23271)]
Debug2
Entering RelaxThread...
Entering RelaxThread 1
[Thread 0x40040940 (LWP 23270) exited]
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 0
Iterating...
Debug1
[Thread 0x40081940 (LWP 23271) exited]
[New Thread 0x40081940 (LWP 23272)]
Debug2
Debug1

Edit2:在更改后更新了回溯日志。

Edit3:下面是完整的 RelaxThread

    void *RelaxThread(void *para) {
        struct Parameter *parameters;
        parameters = (struct Parameter *) para;
        double **matrix = parameters->matrix;
        int size = parameters->size;
        double precision = parameters->precision;
        int threads = parameters->threads;
        int threadId = parameters->threadId;
        int startRow = parameters->startRow;
        int allocation = parameters->allocation;
        int finishRow = (startRow + allocation);
        int i;
        int j;
        double oldValue;
        double newValue;
        double difference;
        for(i=startRow;i<finishRow;i++) {
            for (j=1;j<size-1;j++) {
                pthread_mutex_lock (&mutexmatrix); //Tested with and without the mutex, which is globally defined and initialised elsewhere
                oldValue = matrix[i][j];
                newValue = ((matrix[i+1][j] + matrix[i-1][j] + matrix[i][j+1] + matrix[i][j-1]) / 4);
                matrix[i][j] = newValue;
                pthread_mutex_unlock (&mutexmatrix);
                difference = oldValue - newValue;
                if(difference < 0) {
                    difference = -difference; 
                }
                //printf("Precision on thread%d: %f , aiming for %f\n",threadId, difference,precision);
                if (difference > precision) {
                    keepRunning = 1;
                }
            }
        }
}

最佳答案

正确的初始化,假设一个平方矩阵是:

double **matrix = malloc(size * sizeof(double *));
for(int i=0; i<size; i++)
{
    matrix[i] = malloc(size * sizeof(double));
    for(int j=0; j<size; j++)
    {
        matrix[i][j] = 0; // removing the junk contents
    }
}

编辑: 我还意识到您实际上从未初始化过 void *status。由于您没有使用它,因此将其更改为 rc = pthread_join(threadcount[t], NULL);,这肯定是另一个问题。

编辑3: 我发现的另一个问题是 int startRow = 1;,我看不出你是如何访问矩阵的,但它的索引应该从 0 开始。

编辑4: 另外,当你释放一个二维数组时,你需要这样做:

for(int i=0; i<size; i++)
{
    free(matrix[i]);    //Free each row pointer
}
free(matrix);

关于C: pthread_create 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8252092/

相关文章:

c++ - C++中,为什么返回不同对象私有(private)变量的指针会导致段错误?

c - C中 double 组的优化总和

计算 tcp 数据包结构中的有效负载

编译线程程序

C pthread,指针丢失内容

c - C中的主线程在一定时间后终止

c - 如何在本地使用 hping 或在 C 中使用 fork?

C 指针指向错误的对象

C/Pointer - 声明一个局部变量指针而不初始化它

c - 几次重新分配后出现段错误 |结构数组