Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c++ - Error with passing a pointer using threads

Updated code: 3/7/11 : 9:29pm

using namespace std;

void * matrixACreate(void * param);  
void *status;

struct a  
{  
     int Arow; // Matrix A    
     int Acol; // WxX  
     int low;  // Range low  
     int high;  
 };  

int main(int argc, char * argv[])
{     
    struct a matrix_mult_info;  

    matrix_mult_info.Arow = atoi(argv[1]); // Matrix A  
    matrix_mult_info.Acol = atoi(argv[2]); // WxX

    matrix_mult_info.low = atoi(argv[5]); // Range low
    matrix_mult_info.high = atoi(argv[6]);

    pthread_t matrixAthread;

    pthread_t runner;  
    int error, retValue;  

    struct a * a = (struct a *) malloc(sizeof(struct a));   
    error = pthread_create(&matrixAthread, NULL, matrixACreate, a );  
    //error = pthread_create(&matrixAthread, NULL, matrixBCreate, sendB);  
    retValue = pthread_join(matrixAthread, &status);  
    //retValue = pthread_join(matrixBthread, &status);  

    return 0;  
}

void * matrixACreate(void * param) {    
    struct a * matrix = (struct a *) param;    
    int range = ((matrix->high - matrix->low) + 1);    
    cout << matrix->Arow << endl;    
    return 0;  
}    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
struct a * a = (struct a *) malloc(sizeof(struct a)); 
// init a's members
error = pthread_create(&matrixAthread, NULL, matrixACreate, a);

EDIT: In response to updated question:

void * matrixACreate(void * param) {  
    struct a * matrix = (struct a *) param;  
    int range = ((matrix->high - matrix->low) + 1);  
    cout << matrix->Arow << endl;  
    return NULL;
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...