2016-11-12 150 views
0

線程對我而言是新的。我只是試過這樣的代碼線程無法正常工作

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <semaphore.h> 
typedef struct{ 
    sem_t *s; 
    int firstID; 
    int secondID; 
} stadium; 
void * game(void * currentData){ 
    stadium * st = (stadium *)currentData; 
sem_wait(st->s); 
int first = st->firstID; 
int second = st->secondID; 
int o = rand(); 
int t = rand(); 
printf("%d Team %d:%d %d Team\n",first,o%100009,t%100009,second); 
sem_post(st->s); 
} 
int main(){ 
for(int i= 1;i<=10;i++){ 
    for(int j = i+1;j<=10;j++){ 
     sem_t s ; 
     sem_t c; 
     sem_init(&s,0,4); 
     sem_init(&c,0,1); 
     pthread_t p; 
     stadium st; 
     st.firstID = i; 
     st.secondID = j; 
     st.s = &s; 
     st.counter = &c; 
     pthread_create(&p,NULL,game,&st); 
    } 
} 
pthread_exit(0); 
return 0; 
} 

它隨機打印,但不知何故它打印相同的對。當它只在同一對上迭代一次時,它如何打印同一對呢?

+2

你傳入* *不同的信號量給每個線程。這打破了信號量的目的,因爲這意味着它們不會彼此同步。另外,信號量在'for'循環中被聲明爲*,因此它們在循環結束後超出範圍(此時線程可能還沒有運行)。最後,你的主線程不會等待子線程退出 - 所以它將在線程退出時終止所有線程。 – kaylum

+3

'體育場st'。這也是一個傳遞給線程的自動變量,但是在子線程可能已經完成或未完成的時間超出了範圍。也就是說,您的代碼充滿了未定義的行爲和邏輯錯誤。 – kaylum

+0

@kaylum主要會讓其他線程完成。 – 2501

回答

0

以下代碼清理編譯器,產生所需的輸出,沒有不正確/重複的輸出值,也不使用信號量。

使用信號量不會導致所有值的輸出順序遞增。

不使用malloc()導致重複和缺失輸出線

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

#define MAX_THREADS (100) 
#define MAX_I  (10) 
#define MAX_J  (10) 

struct myStadium 
{ 
    int firstID; 
    int secondID; 
}; 

typedef struct myStadium stadium; 


//sem_t s ; 


void * game(void * dummy) 
{ 
    stadium *st = (stadium*)dummy; 

    //sem_wait(&s); 
    int o = rand(); 
    int t = rand(); 
    printf("%d Team %d:%d %d Team\n", 
      st->firstID, 
      o%100009, 
      t%100009, 
      st->secondID); 
    free(st); 
    //sem_post(&s); 
    pthread_exit(NULL); 
} // end thread: game 

pthread_t p[100] = {0}; 

int main(void) 
{ 
    //sem_init(&s,0,4); 

    for(int i=1; i<=MAX_I; i++) 
    { 
     for(int j=i+1; j<=MAX_J; j++) 
     { 
      //sem_wait(&s); 
      stadium *st = malloc(sizeof(stadium)); 
      if(!st) 
      { 
       perror("malloc failed"); 
      } 

      else 
      { // else, malloc successful 
       st->firstID = i; 
       st->secondID = j; 
       if(0 != pthread_create(&p[i*j],NULL,game, st)) 
       { 
        perror("pthread_create failed"); 
        free(st); 
       } 
      } 
      //sem_post(&s); 
     } 
    } 

    for(int k = 0; k<MAX_THREADS; k++) 
    { 
     if(p[k]) 
     { 
      pthread_join(p[k], NULL); 
     } 
    } 

    int status = 0; 
    pthread_exit(&status); 

} 

與上面的代碼,輸出爲:

1 Team 27014:54674 3 Team 
1 Team 41442:82619 5 Team 
1 Team 71618:157 4 Team 
1 Team 20604:12028 6 Team 
1 Team 62973:34366 7 Team 
1 Team 10103:68500 8 Team 
1 Team 98202:20843 9 Team 
1 Team 13740:36869 10 Team 
2 Team 57690:44808 3 Team 
2 Team 61812:38439 4 Team 
2 Team 2061:48433 5 Team 
2 Team 76053:1017 6 Team 
2 Team 35506:44049 8 Team 
2 Team 97788:44099 7 Team 
2 Team 81026:60961 9 Team 
2 Team 14803:17640 10 Team 
3 Team 15627:65854 4 Team 
3 Team 9859:96854 5 Team 
9 Team 61159:30004 10 Team 
3 Team 66011:30464 6 Team 
3 Team 18482:28975 7 Team 
3 Team 74439:28585 8 Team 
3 Team 7075:72632 9 Team 
3 Team 59037:30425 10 Team 
4 Team 19102:16718 5 Team 
4 Team 84842:80914 6 Team 
4 Team 64767:86903 7 Team 
4 Team 38948:40811 8 Team 
4 Team 87921:74454 9 Team 
4 Team 94469:95309 10 Team 
5 Team 18544:85095 6 Team 
5 Team 56261:33347 7 Team 
5 Team 2727:71888 8 Team 
5 Team 8802:22195 9 Team 
6 Team 78342:74813 7 Team 
6 Team 62268:96824 8 Team 
5 Team 13389:46308 10 Team 
6 Team 35009:20464 9 Team 
6 Team 18931:94047 10 Team 
7 Team 60498:47642 9 Team 
7 Team 20365:45332 10 Team 
7 Team 38157:94741 8 Team 
8 Team 32226:77105 9 Team 
8 Team 35543:29747 10 Team 
1 Team 25047:79703 2 Team