1

行列のファイルを読み取り、2 つの行列の各行を乗算するスレッドを作成するマルチスレッド プログラムを作成しています。単一のスレッドのみを使用してすべてを適切に動作させることができましたが、複数のスレッドを同時に作成しようとすると、問題が発生しました。まず、結果の行列を、行列値の配列を含む構造体の配列を含むグローバル構造体に格納しています。(例: struct matrixArray.array -> struct matrix.array)。pthread_create によって呼び出されていた関数が実行されると、グローバルな matrixArray のすべてのデータが失われました。そこで、pthread_create によって呼び出される関数に matrixArray を渡そうとしました。ただし、エラーが発生し続けます。式には構造体または共用体の型が必要です。これは、呼び出されている関数内の構造体にアクセスしようとしたときです。

ここに私の構造があります:

typedef struct matrix{

    int rows;
    int cols;
    int multRow; // The "MULTIPLIED ROW" This is for determing which row the current thread needs to use for multiplication. This only applies for Matrix A in each set.
    int size;
    int set; // This is for which set the matrix belongs to.
    char letter; // This is for labeling the matrices A B and C
    int * array;
    unsigned int * threadID; // Array containing the thread ids that are used to create the result

} matrix;


typedef struct matrixArray{

    int size;
    matrix * array;

} matrixArray;

主な機能は次のとおりです。

int main(int argc, char *argv[])
{

    pthread_t * tid; /* the thread identifier */
    pthread_attr_t attr; /* set of attributes for the thread */
    int i; // Counter
    int aIndex; // Index of the current 'A' matrix being multiplied.
    int rows,cols;

    // Checke to make sure we have the correct number of arguments supplied
    // when running the program.
    if(argc < 1){
            printf("Error: You did not provide the correct number of arguments.\n\n");
            return 0;
    }

    // Read the file and create the matrices
    readFile();


    // Initialize the result matrix before we start creating threads that use it.
    mtxResults = newMatrixArray();

    // Get the default attributes
    pthread_attr_init(&attr);

    // Set the current set to be mutliplied to 1
    currentSet = 1;

    // Create a new matrixArray to pass to the threads
    //struct matrixArray *mtxPassed = malloc(sizeof(struct matrixArray));
    struct matrixArray *mtxPassed = newMatrixArray();
    memcpy(mtxPassed, &mtxResults, sizeof(struct matrixArray));

    // Allocate size of tid array based on number of threads
    tid = malloc(threads * sizeof(pthread_t));

    // Create the threads.
    for(i = 0; i < threads; i++){
            //pthread_create(&tid[i], &attr, runner, argv[1]);
            pthread_create(&tid[i], &attr, runner, mtxPassed);

            // Increment currentSet when the current row evalutated
            // in the current set is equal to the total number of rows available.
            aIndex = ((currentSet * 2) - 2);

            if(mtx.array[aIndex].multRow == mtx.array[aIndex].rows){
                    currentSet++;
            }
    }

    // Wait for threads to finish
    for(i = 0; i < threads; i++){
            pthread_join(tid[i], NULL);
    }

    // Print the matrices
    //printMatrices();

} // End of main()

pthread_create によって呼び出される関数 runner は次のとおりです。

// The thread will begin control in this function
void *runner(void *param)
{

    struct matrixArray *mtxA = newMatrixArray();
    mtxA = (struct matrixArray *)param;

    printf("mtxPassed.size = %i\n",mtxA.size);

    // Do the matrix multiplication for a single row
    matrixMultiply(currentSet, (unsigned int)pthread_self(), mtxA);

    pthread_exit(0);

}

元々、mtxA構造体をmatrixMultiply関数に渡したときにエラーが発生していましたが、ランナー内で直接アクセスして、そこで機能するかどうかを確認しました. まだそうではありません。「printf("mtxPassed.size = %I\n",mtxA.size);」でエラーが発生します。ランナーの最初の 2 行の代わりに、「struct matrixArray *mtxA = (struct matrixArray *)param;」も試しました。それもうまくいきませんでした。

エラーは次のとおりです。

[nsltg2@lewis assign2]$ cc -pthread -lpthread assign2.c
assign2.c(602): error: expression must have struct or union type
  printf("mtxPassed.size = %i\n",mtxPassed.size);
                             ^

あなたが提供できる助けを本当に感謝します。どうもありがとう!

4

0 に答える 0