2016-03-08 47 views
1

我有這個C程序,它必須以0-6的順序顯示線程。我正在使用互斥鎖,但是當我嘗試運行我的代碼時,沒有任何反應,沒有任何顯示。此外,編譯器顯示沒有錯誤使用互斥對訂單中的線程進行同步

我用鎖和解鎖互斥量,但我不知道如果我在正確的地方創建它。 任何建議和幫助表示讚賞。

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


void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start   threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 


int main() 
{ 
int i; 
pthread_t tid[7]; 
// Initialize random number generator 
time_t seconds; 
time(&seconds); 
srand((unsigned int) seconds); 

int rc; 



// Create our threads 
for (i = 0; i < 7; i++) 
{ 
    pthread_create(&tid[i], NULL, text, (void*)code[i]); 

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

    { rc = pthread_mutex_lock(&a_mutex); 

     for (i = 0; i < 7; i++) 
     { 
      rc = pthread_mutex_unlock(&a_mutex); 
     } 
    } 

} 
//join threads 
for (i=0; i<7; i++) 
{ 
    if (pthread_join(tid[i], NULL)); 
       } 

rc = pthread_mutex_destroy(&a_mutex); 

// Exit main 
return 0; 

} 
void *text(void *arg) 
{ 
long n = (long)arg; 
int rand_sec = rand() % (3 - 1 + 1) + 1; // Random num seconds to  sleep 
while (num != n) {} // Busy wait used to wait for our turn 
num++; // Let next thread go 
sleep(rand_sec); // Sleep for random amount of time 
printf("This is thread %d.\n", n); 
// Exit thread 
pthread_exit(0); 
} 

回答

1

找出問題的關鍵是問自己「問題是我的6個線程中共享的數據是什麼?」 是在鎖定的互斥體塊中需要互斥保護(讀取和寫入)的變量。目前,您只能鎖定和解鎖來自單個主線程的互斥鎖,這實際上什麼都不做。

你可能想要的東西更接近這個(雖然這可以大大簡化 - 例如,可以徹底刪除休眠):

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

void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 

int main() 
{ 
    int i; 
    pthread_t tid[7]; 
    // Initialize random number generator 
    time_t seconds; 
    time(&seconds); 
    srand((unsigned int) seconds); 
    // Create our threads 
    for (i = 0; i < 7; i++) 
     pthread_create(&tid[i], NULL, text, (void*)code[i]); 

    //join threads 
    for (i=0; i<7; i++) 
     if (pthread_join(tid[i], NULL)); 

    pthread_mutex_destroy(&a_mutex); 

    // Exit main 
    return 0; 
} 

void *text(void *arg) 
{ 
    long n = (long)arg; 
    long localNum = -1; 
    int rand_sec = rand() % (3 - 1 + 1) + 1; // Random num seconds to  sleep 
    int rc; 
    while (localNum != n) 
    { 
     rc = pthread_mutex_lock(&a_mutex); 
     localNum = num; 
     rc = pthread_mutex_unlock(&a_mutex); 
     sleep(rand_sec); // Sleep for random amount of time 
    } 

    printf("This is thread %d.\n", n); 

    rc = pthread_mutex_lock(&a_mutex); 
    num++; // Let next thread go 
    rc = pthread_mutex_unlock(&a_mutex); 

    pthread_exit(0); 
} 
1

這裏去的代碼的最小版本,去掉了等待和隨機化因爲它們將注意力從鎖的工作方式轉移。

線程自動排隊等待互斥鎖。

您需要詢問常規功能中的鎖。

一定要記住關閉線程並釋放所有可能的代碼路徑上的鎖。

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


void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start   threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 


int main() 
{ 
int i; 
pthread_t tid[7]; 

// Create our threads 
for (i = 0; i < 7; i++) 
{ 
    pthread_create(&tid[i], NULL, text, (void*) & code[i]); 
} 



//join threads 
for (i=0; i<7; i++) 
{ 
    pthread_join(tid[i], NULL); 
} 

pthread_mutex_destroy(& a_mutex); 

// Exit main 
return 0; 
} 

void *text(void *arg) 
{ 
while(1){ 
    pthread_mutex_lock(&a_mutex); 
    if (num == *(int *) arg){ 
     printf("This is thread has the code %d\n",*(int *) arg); 
     num++; 
     pthread_mutex_unlock(&a_mutex); 
     pthread_exit(0); 
     } 
    pthread_mutex_unlock(&a_mutex); 
    } 
} 
0

@Anastasia Netz。您只需在代碼的關鍵部分使用pthread_mutex_lock and pthread_mutex_unlock互斥鎖,而不是僅在主線程中使用。請在您的代碼中仔細確定您的關鍵部分(部分,其中重要的工作已完成並且對所有正在執行的線程都很常見,但其中只有一個可以訪問),並在那裏使用這些互斥鎖。 現在讓我來解釋一下你的代碼: 在你的代碼中,你只需創建7個線程。在創建每個線程後,您只需鎖定一次互斥鎖,並無故解鎖七次。因此,在所有七次迭代完成之後(for創建了這七個線程),您已經鎖定了七次互斥鎖,並且已經爲(7 * 7 = 49)四十九次解鎖了互斥鎖。 這裏是做這件工作的代碼:

for (i = 0; i < 7; i++) { pthread_create(&tid[i], NULL, text, (void*)code[i]);

for (i = 0; i < 7; i++) 
{ 
rc = pthread_mutex_lock(&a_mutex); 

    for (i = 0; i < 7; i++) 
    { 
     rc = pthread_mutex_unlock(&a_mutex); 
    } 
}} 

請參閱Xvan的代碼。

相關問題