首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >导致段缺省错误的Pthread_join()

导致段缺省错误的Pthread_join()
EN

Stack Overflow用户
提问于 2013-02-09 00:27:44
回答 1查看 166关注 0票数 1

下面的代码要完成的就是计算A和B的矩阵乘法来得到矩阵C。它使用nXn线程独立地计算C的每个条目。所以代码可以在Cygwin上运行,但不能在linux上运行。我不断得到Pthread_join调用的默认段。

代码语言:javascript
运行
复制
#define _REENTRANT // Make sure the library functions are MT (muti-thread) safe
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define BUFFER_SIZE 512

// Declare a structure data type that will be used to pass arguments to the worker threads
typedef struct args_for_thread_t{
         int *rowA;
         int rowIdx;
         int *columnB;
         int columnIdx;
         int **matrixC;

} ARGS_FOR_THREAD;
/* Global Variables */
int numRows,numColumns;

/*Function Prototype*/
void *computeC(void *this_arg);
void printMatrix(int** matrix,int numRows,int numColumns);

int main(void){
    const char filename[] = "input_data.txt";
    FILE *file = fopen(filename,"r");
    char *delims = " ";
    int **matrixA,**matrixB,**matrixC;
    int flagB = 0; //Indicate wether the program should process matrixB
    int i,j;

    if (file){
        char line[BUFFER_SIZE];
        int rowIdx = 0;
        while (fgets(line,sizeof(line), file)){
            char substr[BUFFER_SIZE], *result;
            //fputs(line,stdout);
            result = strtok(line, delims);
            int columnIdx = 0;

            //Once we reach a line break, we start the processing of matrix B
            if (!strcmp(line,"\n")){
                flagB = 1;
                rowIdx = 0; //Reset the rowIdx
                continue; //Skip the new line, and start to read data into matrix B
            }
            while (result != NULL){
                if (!strcmp(result,"ROWS")){ //To retrieve the number of rows
                  result = strtok(NULL,delims);
                  numRows = atoi(result);
                  matrixA = (int **) malloc(numRows*sizeof(int*));
                  matrixB = (int **) malloc(numRows*sizeof(int*));
                  matrixC = (int **) malloc(numRows*sizeof(int*));
                  rowIdx = -1;
                  result = strtok(NULL,delims);
                }
                else if (!strcmp(result,"COLUMNS")){//To retrieve the number of Columns
                  result = strtok(NULL,delims);
                  numColumns = atoi(result);
                  for (i=0;i<numRows;i++){ //Malloc the columns
                    matrixA[i] = (int *) malloc(numColumns*sizeof(int));
                    matrixB[i] = (int *) malloc(numColumns*sizeof(int));
                    matrixC[i] = (int *) malloc(numColumns*sizeof(int));
                  }
                  rowIdx = -1;
                  result = strtok(NULL,delims);
                }
                else if (!flagB){ //Processing Matrix A
                    matrixA[rowIdx][columnIdx] = atoi(result);
                    columnIdx++;
                    result = strtok(NULL,delims);
                }
                else if (flagB){ //Processing Matrix B
                    matrixB[rowIdx][columnIdx] = atoi(result);
                    columnIdx++;
                    result = strtok(NULL,delims);
                }
            }
            rowIdx++;
        }
    }
    else{
        printf("No Such File exists!\n");
    }
    //At this point, matrix A and matrix B are both ready for computation. We will start to compute the product of the two matrices
    int num_threads = numRows*numColumns; //The toal number of worker threads
    pthread_t *worker_thread = (pthread_t *) malloc(sizeof(pthread_t)*num_threads);
    ARGS_FOR_THREAD *args_for_thread;


    for(i = 0; i < numRows; i++){
        for(j = 0; j < numColumns; j++){
            args_for_thread = (ARGS_FOR_THREAD *)malloc(sizeof(ARGS_FOR_THREAD)); // Allocate memory for the structure that will be used to pack the arguments
            args_for_thread-> rowA = matrixA[i]; 
            //We need to allocate the corresponding column in B for multiplication
            int k;
            args_for_thread->columnB =(int *) malloc(sizeof(int)*numRows);
            for (k=0;k<numRows;k++){
                args_for_thread-> columnB[k] = matrixB[k][j];
            }
            //rowIdx and columnIdx gives the corresponding entry for matrix C
            args_for_thread-> rowIdx = i;
            args_for_thread-> columnIdx = j;
            args_for_thread-> matrixC = matrixC;            
            if((pthread_create(&worker_thread[i], NULL, computeC, (void *)args_for_thread)) != 0){
                printf("Cannot create thread \n");
                exit(0);
            }
        }
    }
    // Wait for all the worker threads to finish 
    for(i = 0; i < num_threads; i++)
        pthread_join(worker_thread[i], NULL);
    //Print out the Final Matrix C
    printMatrix(matrixC,numRows,numColumns);
    //Clean up pointers
    for(i = 0; i < numRows; i++){
        free(matrixA[i]);
        free(matrixB[i]);
        free(matrixC[i]);
    }
    free(matrixA);
    free(matrixB);
    free(matrixC);
}
void printMatrix(int** matrix,int numRows, int numColumns){
    int i,j;
    for (i=0;i<numRows;i++){
        for (j=0;j<numColumns;j++){
            printf("%d ",matrix[i][j]);
            if (j==numColumns-1){
                printf("\n");
            }
        }
    }   
}
/* Function that will be executed by all the worker threads. It will compute the i,j entry for column C */
void *computeC(void *this_arg){
    ARGS_FOR_THREAD *arg = (ARGS_FOR_THREAD *) this_arg;
    int rowIdx = arg->rowIdx;
    int columnIdx = arg->columnIdx;
    int *rowA = arg->rowA;
    int *columnB = arg->columnB;
    int **matrixC = arg->matrixC;
    int i;
    int sum = 0;

    for (i=0;i<numRows;i++){  //Compute entry for matrix C. Since A,B are nxn square matrix, we can use either numRows or numColumns as the size
        sum += rowA[i]*columnB[i];
    }
    matrixC[rowIdx][columnIdx] = sum;
    free((void *) arg); // Free up the structure
    pthread_exit(NULL);
}

这里的问题是什么?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-09 00:45:55

这里:

代码语言:javascript
运行
复制
pthread_create(&worker_thread[i] ...

您创建了i*j个线程,但是您只提供了worker_threadsi,因此您的程序一直使用相同的pthread_t变量。稍后,当您尝试连接具有未定义的pthread_t值的线程时,它会失败。

替换为:

代码语言:javascript
运行
复制
pthread_create(&worker_thread[i*numColumns+j] ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14777026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档