0

CalcTerm 函数是问题所在,特别是当我尝试访问 m1->d[n][m] 或类似的东西时。

当我在单核 WINDOWS 平台上运行相同的代码时(或至少看起来是这样)。但是,当我在 Unix 实验室尝试时,它给了我分段错误。

#include <iostream>
#include <cstdlib>
#include <pthread.h>
using std::cout;
using std::endl;
#define SIZE 3

//*******************STRUCTS AND GLOBAL VARIABLES*****************************//

struct Matrix
{
       int d[SIZE][SIZE];
       };


Matrix* matrix_addr[SIZE]; // array to store the address of the matrices

int n;
int m;

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t my_cond = PTHREAD_COND_INITIALIZER;



//****************************PRINT FUNCTIONS*********************************//

void print(Matrix* m)
{
     ...
}

//............................................................................//

void print_result(Matrix* m0, Matrix* m1, Matrix* m2)
{
     ...
}



//****************************THREAD******************************************//

void* calcTerm(void* arg)
{
    int sum = 0;

    int* matptr = (int*) arg; 
    void* mat0 = (void*)(*((Matrix*)matptr)).d[0][0];

    matptr = (int*) arg+1;
    void* mat1 = (void*)(*((Matrix*)matptr)).d[0][0];

    matptr = (int*) arg+2;
        void* mat2 = (void*)(*((Matrix*)matptr)).d[0][0];

    Matrix* m0 = (Matrix*) mat0;
    Matrix* m1 = (Matrix*) mat1;
    Matrix* m2 = (Matrix*) mat2;

    cout << endl << "Inside thread\n" ;

    pthread_mutex_lock(&my_mutex);

    for (int i = 0; i < SIZE; ++i)
    {
        cout << "\ni = " << i << "\tn = " << n << "\tm = " << m << endl;
        sum =  sum + m1->d[n][i] * m2->d[i][m];
    } 

    m0->d[n][m] = sum;

    // update n and m for the next thread


          if (m < 2) 
             m++;
          else
             m = 0;
          if (n < 2) 
             n++;
          else
             n = 0;


     //pthread_cond_signal(&my_cond);
     pthread_mutex_unlock(&my_mutex);
     cout << endl << "Going out of thread\n" ;
     pthread_exit(NULL);
}



//********************************MAIN****************************************//

int main()

{
    Matrix m0, m1, m2; //Matrices are 3x3; 
                       // m0 <= m1 * m2
    pthread_t id[9];   // 3x3 matrix multiplication requires 9 threads.

    matrix_addr[0] = &m0;     // the pointers to the matrices are stored here.
    matrix_addr[1] = &m1;
    matrix_addr[2] = &m2;


    n = m = 0;       // initialize the global variable
    srand(time(NULL));  // seed rand()




    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            m0.d[i][j] = 0;           // m0 is being cleared for the output
            m1.d[i][j] = rand()%10;    // m1 and m2 are generated with rand()
            m2.d[i][j] = rand()%10;

        }
    }
    //display the input matrices
    cout << "MATRIX 1:\n\n";
    print (&m1);
    cout << "\nMATRIX 2:\n\n";
    print (&m2);
    cout << "\nMATRIX 3:\n\n";
    print (&m0);



    for (int i = 0; i < SIZE*SIZE; i++) // run all the threads for calculating each output
    {

    cout << endl << "Going in to thread " << i ;
        pthread_create(&id[i], NULL, calcTerm, (void*) &matrix_addr);

    cout << endl << "Out of thread " << i ;
    //pthread_join(id[i], NULL);
        }    
    //pthread_cond_wait(&my_cond, &my_mutex);

    print_result (&m0, &m1, &m2);
    return 0;



}

顺便说一句,我还没有使用 pthread_join(),因为我不确定我应该如何使用它。我欢迎尽可能多的建议。

哦,我还注释掉了 pthread_cond_stuff; 如果可以的话,我宁愿不使用它。

谢谢。

4

0 回答 0