2013-05-12 61 views
0

我想用互斥鎖完成我的線程。第一個線程不會執行,線程2執行。無法用互斥鎖執行第一個線程

有誰知道這個問題可能是什麼?有時會執行線程1,但不執行2或3。我不知道這裏有什麼問題。

Thread created successfully 
    Thread created successfully 
    Thread created successfully 
    ---------------------------------------------------- 
    J:0 
    NUM_REQUESTS (before function): 0 
    J:0 
    ---------------------------------------------------- 
    ---------------------------------------------------- 
    J:1 
    Third thread processing done 
    WRITE DATA TO LIST! 
    NUM_REQUESTS(function): 1 
    NUM_REQUESTS (before function): 0 
    J:1 
    ---------------------------------------------------- 
    ---------------------------------------------------- 
    J:2 
    Second thread processing done 
    WRITE DATA TO LIST! 
    NUM_REQUESTS(function): 0 
    NUM_REQUESTS (before function): 0 
    J:2 
    ---------------------------------------------------- 

計劃:

#include<stdio.h> 
#include<string.h> 
#include<pthread.h> 
#include<stdlib.h> 
#include<unistd.h> 

pthread_mutex_t request_mutex = PTHREAD_MUTEX_INITIALIZER; 
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER; 

pthread_t tid[3]; 
int thr_id[3]; 
int ret1,ret2,ret3; 
int i = 0; 
int err; 
int *ptr[2]; 
int num_requests = 0; 
int rc = 0; 

這是線程的功能,不執行第一線!

void* doSomeThing(void *arg) 
{ 
    unsigned long i = 0; 
    pthread_t id = pthread_self(); 

    for(i=0; i<1000000;i++); 

    if(pthread_equal(id,tid[0])) 
    { 
     printf("First thread processing done\n"); 
     printf("WRITE DATA TO LIST!\n"); 
     num_requests--; 
     printf("NUM_REQUESTS(function): %d\n",num_requests); 
     rc = pthread_mutex_unlock(&request_mutex); 
     pthread_exit(&tid[0]); 
    }else if(pthread_equal(id,tid[1])){ 

     printf("Second thread processing done\n"); 
     num_requests--; 
     printf("WRITE DATA TO LIST!\n"); 
     printf("NUM_REQUESTS(function): %d\n",num_requests); 
     rc = pthread_mutex_unlock(&request_mutex); 
     pthread_exit(&tid[1]); 
    }else if(pthread_equal(id,tid[2])){ 

     printf("Third thread processing done\n"); 
     printf("WRITE DATA TO LIST!\n"); 
     printf("NUM_REQUESTS(function): %d\n",num_requests); 
     num_requests--; 
     rc = pthread_mutex_unlock(&request_mutex); 
     pthread_exit(&tid[2]); 
    } 
    return NULL; 
} 

這是我創建的線程的輸出

void add_request(int j,pthread_mutex_t* p_mutex,pthread_cond_t* p_cond_var) 
{ 
    printf("----------------------------------------------------\n"); 
    printf("J:%d\n",j); 
    if(num_requests > 3){ 
     printf("WAIT TILL THREADS ARE FREE!\n"); 
    }else{ 

    rc = pthread_mutex_lock(&request_mutex); 
    printf("NUM_REQUESTS (before function): %d\n",num_requests);  

    num_requests++; 
    rc = pthread_mutex_unlock(&request_mutex); 

    rc = pthread_mutex_lock(&request_mutex); 
    rc = pthread_cond_signal(p_cond_var); 
    printf("J:%d\n",j); 
    printf("----------------------------------------------------\n"); 
    } 
} 

在主我只創建線程,使用add_request函數來執行線程

int main(void) 
{ 
    //create 3 threads 
    while(i < 3) 
    { 
    thr_id[i] = i; 
     err = pthread_create(&(tid[i]), NULL, &doSomeThing, (void*)&thr_id[i]); 
     if (err != 0) 
      printf("can't create thread :[%s]", strerror(err)); 
     else 
      printf("Thread created successfully\n"); 

     i++; 
    }  

    int j; 
    for(j=0;j<3;j++){ 
    add_request(j, &request_mutex, &got_request); 
    } 


    return 0; 
} 

回答

1

是什麼你想做什麼?

至少有一個問題,我看到它,你解鎖互斥沒有鎖定它...

當你創建一個線程它自發開始(或多或少),所以你不能假設add_request將被調用之後doSomething()

您可以鎖定然後解鎖。如果您想等待線程完成,請考慮使用ptheard_join

編輯 這是你想從https://computing.llnl.gov/tutorials/pthreads/

好運

+0

https://computing.llnl.gov/tutorials/pthreads/samples/condvar.c -taken我鎖NUM_REQUESTS互斥。 之後,我解鎖。 然後我再次鎖定互斥鎖來處理我的線程。 在那個函數中,我再次解鎖互斥鎖。 我不明白你的意思?我也是多線程新手..我總是鎖定和解鎖互斥鎖。不? 並感謝您的提示。將從現在開始使用pthread。 謝謝。 – 2013-05-12 09:09:38

+0

我只想完成每個輸入的請求。所以這是一個簡單的例子來開始學習它。所以我想要做的第一件事就是完成3個線程 – 2013-05-12 09:11:08

+0

您需要了解可以調用doSomeThing * BEFORE *添加請求 – 2013-05-12 17:04:13

相關問題