c - 线程工作不正常

标签 c multithreading struct

线程对我来说是新事物。我刚刚尝试了这样的代码

#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;
 }

它随机打印,但不知何故它打印相同的对。当它只在同一对上迭代一次时,如何打印同一对?

最佳答案

以下代码干净的编译器,产生所需的输出,没有不正确/重复的输出值,并且不使用信号量。

使用信号量不会导致所有值的输出顺序递增。

不使用 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

关于c - 线程工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567473/

相关文章:

c - 访问 typedef 结构数组

java - 在其他线程中等待 Messagebox 的答复

c# - 事件在错误的线程中?

c++ - Qt:对于生产者-消费者模式中的消费者来说,这是正确的退出方式吗?

C - 在结构中声明和初始化数组

c++ - 订购结构成员

c - 信息 C5012 : loop not parallelized due to reason '1008'

c - Valgrind 提示 inet_pton()

c++打印包含结构的 vector

c++ - 寻找将 C/C++ 中的多行 "block"注释转换为单行注释的脚本