2016-11-12 185 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