2016-11-12 114 views
2

我的程序所需的功能:
使用命令行用戶輸入NMN是將要創建的新線程的數量,並且M是每個線程增加全局變量的數量A爲什麼使用線程時我的程序輸出總是不一樣?

這是我的代碼:

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

static int A = 0; 

void *Thread(void* x){ 
     int i; 
     int n = *((int*)x); 
     for (i = 0; i<n; i++){ 
       A++; 
     } 
} 

int main(int argc, char* argv[]){ 
     int i; 
     int N = atoi(argv[1]); 
     int M = atoi(argv[2]); 

     pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t)*N); 

     if(!thread){ 
       printf("No memory\n"); 
       exit(2); 
     } 

     for (i = 0; i< N; i++){ 
       if (pthread_create(&thread[i], NULL, Thread, &M)){ 
         printf("Not able to create a thread\n"); 
         exit(1); 
       } 
     } 

     for(i = 0; i< N; i++) 
       pthread_join(thread[i], NULL); 

     printf("A = %d\n", A); 

     return 0; 
} 

的問題是,我每次運行它的時候有不同的輸出。 Screenshot of my terminal when i run the program multiple times in a row

+0

應當保護併發讀/寫訪問變量(此處爲'A')。例如通過使用互斥鎖。 – alk

+0

OT:這個強制轉換'(pthread_t *)'在C中沒用。或者你應該使用C++編譯器嗎? – alk

+0

關於使用互斥鎖:http://stackoverflow.com/q/34524/694576 – alk

回答

3

問題是,您正在創建多個線程,並行嘗試修改您的靜態全局變量在同一時間,沒有任何形式的保護。

這意味着根據線程的調度,全局變量上的更改不會是原子的,會產生這種效果。

您可以用互斥解決這個問題,有其聲明:

pthread_mutex_t mutex; 

並初始化/與調用pthread_mutex_init和pthread_mutex_destroy釋放。

在執行更改之前,在線程內部保護要用pthread_mutex_lock更改的資源,並使用pthread_mutex_unlock將其釋放。因此代碼將如下更改:

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

static int A = 0; 
pthread_mutex_t mutex; 


void *Thread(void* x){ 
     int i; 
     int n = *((int*)x); 

     pthread_mutex_lock(&mutex); 
     A += n; 
     pthread_mutex_unlock(&mutex); 
} 

int main(int argc, char* argv[]){ 
     int i; 
     int N = atoi(argv[1]); 
     int M = atoi(argv[2]); 

     pthread_mutex_init(&mutex, NULL); 
     pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t)*N); 

     if(!thread){ 
       printf("No memory\n"); 
       exit(2); 
     } 

     for (i = 0; i< N; i++){ 
       if (pthread_create(&thread[i], NULL, Thread, &M)){ 
         printf("Not able to create a thread\n"); 
         exit(1); 
       } 
     } 

     for(i = 0; i< N; i++) 
       pthread_join(thread[i], NULL); 

     printf("A = %d\n", A); 

     pthread_mutex_destroy(&mutex); 

     return 0; 
} 
+0

*至少應調用* lock和* unlock來調用失敗。 – alk

+0

一個建議:在Thread()中爲什麼我們需要for循環?我們可以做A + = n。 – MayurK

+0

根本不需要,我想他只是在玩代碼作爲例子。 – maki

相關問題