2017-04-30 96 views
0

例如,我想創建5個線程並打印它們。我如何在第二個之前執行第四個?我試着用互斥體鎖定它,但我不知道如何只鎖定第二個,所以它給了我分段錯誤。如何強制一個線程在另一個之前啓動?

char name1[]="THREAD1"; 
char name2[]="THREAD2"; 
char name3[]="THREAD3"; 
char name4[]="THREAD4"; 
char name5[]="THREAD5"; 

pthread_mutex_t mutex; 

pthread_t t1, t2,t3,t4,t5; 
int* state[5]={0,0,0,0,0}; 

void* execThread(void* threadName) 
{ 
int i; 
int* number = (int*) malloc(sizeof(int)); 
char* tmp; 

if(*(state[4]) == 0 && (pthread_self()==t2)) 
pthread_mutex_lock (&mutex); 
else if(*(state[4]) == 0 && (pthread_self()!=t2)) 
{ 

pthread_yield(); 
printf("[Thread %s] Executing step %d\n", (char *) threadName, i); 

tmp = & ((char*)threadName)[strlen((char*)threadName)-1]; 
*number = atoi(tmp); 

return number; 
} 
else 
{ 
    pthread_mutex_unlock (&mutex); 

    pthread_yield(); 
    printf("[Thread %s] Executing step %d\n", (char *) threadName, i); 


    tmp = & ((char*)threadName)[strlen((char*)threadName)-1]; 
    *number = atoi(tmp); 

    return number; 
} 
} 


void main() 
{ 

pthread_mutex_init(&mutex, NULL); 


printf("[Thread MAIN] Starting. tid=%ld, pid=%d\n", (long int) pthread_self(), (int) getpid()); 

if (pthread_create(&t1, NULL, execThread, name1)!=0) { 
perror("Error creating a new thread"); 
exit(1); 
} 

if (pthread_create(&t2, NULL, execThread, name2)!=0) { 
perror("Error creating a new thread"); 
exit(1); 
}  

if (pthread_create(&t3, NULL, execThread, name3)!=0) { 
perror("Error creating a new thread"); 
exit(1); 
} 
if (pthread_create(&t4, NULL, execThread, name4)!=0) { 
perror("Error creating a new thread"); 
exit(1); 
} 
if (pthread_create(&t5, NULL, execThread, name5)!=0) { 
perror("Error creating a new thread"); 
exit(1); 
} 


printf("[Thread MAIN] Created 5 threads \n"); 

pthread_join(t1, (void**) &state[0]); 
pthread_join(t2, (void**) &state[1]); 
pthread_join(t3, (void**) &state[2]); 
pthread_join(t4, (void**) &state[3]); 
pthread_join(t5, (void**) &state[4]); 

printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name1, (long int) t1, *(state[0])); 
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name2, (long int) t2, *(state[1])); 
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name3, (long int) t3, *(state[2])); 
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name4, (long int) t4, *(state[3])); 
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name5, (long int) t5, *(state[4])); 

printf("[Thread MAIN] Terminating. tid=%ld, pid=%d\n", (long int) pthread_self(), (int) getpid()); 


pthread_mutex_destroy(&mutex); 
//pthread_exit(NULL); 
} 
+0

在創建任何線程之前鎖定互斥鎖。使用更好的機制來告訴線程哪一個是哪個線程 - 將線程號作爲傳遞給線程函數的信息(或其中的一部分)傳遞給線程函數。第二個線程等待鎖定互斥鎖;第四個線程在解鎖互斥體並讓第二個線程繼續之前做它需要做的任何事情。目前尚不清楚這是否應該是線程4的第一件事,或者是最後一個,或者介於兩者之間。線程1,3,5根本不需要大驚小怪。在Linux系統上,'void main()'是無條件錯誤的。 –

+0

與[this](http://stackoverflow.com/q/43707812/1475978)問題完全相同的問題和代碼片段,儘管代碼段已從其他問題中刪除。這裏發生了什麼? –

回答

0

如果要螺紋2只螺紋4已打印它的消息後打印它的消息,則可以使用一個標誌和條件變量。請注意,您無法安全地訪問線程中的t1t2等變量而無需進一步同步(它們正在由主線程寫入,並且您正嘗試從其他線程讀取它們)。

int t4_done = 0; 
pthread_mutex_t t4_done_mutex = PTHREAD_MUTEX_INITIALIZER; 
pthread_cond_t t4_done_cond = PTHREAD_COND_INITIALIZER; 

void* execThread(void* threadName) 
{ 
    if (threadName == name2) 
    { 
     /* thread2 waits for thread4 to set the t4_done flag */ 
     pthread_mutex_lock(&t4_done_mutex); 
     while (!t4_done) 
      pthread_cond_wait(&t4_done_cond, &t4_done_mutex); 
     pthread_mutex_unlock(&t4_done_mutex); 
    } 

    printf("[Thread %s] Executing step\n", (char *) threadName); 

    if (threadName == name4) 
    { 
     /* thread4 sets the t4_done flag and signals thread2 */ 
     pthread_mutex_lock(&t4_done_mutex); 
     t4_done = 1; 
     pthread_cond_signal(&t4_done_cond); 
     pthread_mutex_unlock(&t4_done_mutex); 
    } 

    return NULL; 
} 
相關問題