在C中对pthread使用互斥锁/解锁和广播

在C中对pthread使用互斥锁/解锁和广播,c,pthreads,mutex,C,Pthreads,Mutex,这是我的第一篇文章,我很兴奋 我的问题是,我正在用C语言创建一个石头、布、剪刀程序,其中父进程创建2个线程。然后,这两条线随机投掷一块石头、一张纸或一把剪刀,并将数值返回给父线程,在父线程处对其进行计数,并返回3轮的结果,然后进行最终计数 我的问题是,我无法让线程正确启动,我让它们在我的thread_function1中等待,但它们只完成了一轮,即使这样,我也没有在结果中获得两个线程。如果有人能给我点启示,我会非常感激的!谢谢 #include <stdlib.h> #include

这是我的第一篇文章,我很兴奋

我的问题是,我正在用C语言创建一个石头、布、剪刀程序,其中父进程创建2个线程。然后,这两条线随机投掷一块石头、一张纸或一把剪刀,并将数值返回给父线程,在父线程处对其进行计数,并返回3轮的结果,然后进行最终计数

我的问题是,我无法让线程正确启动,我让它们在我的thread_function1中等待,但它们只完成了一轮,即使这样,我也没有在结果中获得两个线程。如果有人能给我点启示,我会非常感激的!谢谢

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>

#define NTHREADS 2
struct timeval tv;
void *thread_function1();
void *thread_function2();
char *guess_string(int g);
int wins[3];
int cmd_ready = 0;
int x1=0, x2=1, count=0;
int  guess, object, turns, i, j, k,l, winner, cmd, go, y;
pthread_mutex_t cv_m;
pthread_mutex_t count_mutex;
pthread_cond_t cv;
int myindex;
int flag; 
int throws[3];
int main(int argc, char *argv[])
{


  wins[0] = 0; wins[1] = 0;
  if ((argc != 2) || ((turns = atoi(argv[1])) <= 0))
    {
      fprintf(stderr,"Usage: %s turns\n", argv[0]);
      return 0;
    }

    pthread_t thread_id1, thread_id2;


    if (pthread_create(&thread_id1, NULL, thread_function1,&x1) != 0)
        perror("pthread_create"),
        exit(1); 

    if (pthread_create(&thread_id2, NULL, thread_function1,&x2) != 0)
        perror("pthread_create"),
        exit(1); 

    printf("Beginning %d Rounds...\nFight!\n", turns);
    printf("Child 1 TID: %d\n", (unsigned int) thread_id1);
    printf("Child 2 TID: %d\n", (unsigned int) thread_id2 );

    for(k=0; k<turns; k++)
    {

            pthread_mutex_lock (&cv_m);
            cmd = go; 
            cmd_ready = 2;
            pthread_cond_broadcast(&cv);    
            pthread_mutex_unlock(&cv_m);
            printf("------------------------\n");
            printf("Round: %d\n", k+1);

            printf("Child %d throws %s!\n",myindex+1, guess_string(myindex));

            pthread_mutex_lock (&count_mutex);

            winner = find_winner(throws[0], throws[1]);
            while(count == 2){
            if(winner >= 0)
            {
                printf("Child %d Wins!\n", winner+1);
                wins[winner]++;
                printf("6\n");
            }else
            {
                printf("Game is a Tie!\n");
            }
            go--;
            count = 0; 
            pthread_mutex_unlock(&count_mutex);
            }
    }

    pthread_join(thread_id1,NULL); 
    pthread_join(thread_id2,NULL);

    printf("------------------------\n");
    printf("------------------------\n");
    printf("Result:\n");
    printf("Child 1: %d\n", wins[0]);
    printf("Child 2: %d\n", wins[1]);
    printf("Ties: %d\n", turns - (wins[0] + wins[1]));
    printf("Child %d Wins!\n", (wins[0] > wins[1]) ? 1 : 2);

    pthread_mutex_destroy(&cv_m);
    pthread_cond_destroy(&cv);
    pthread_exit(NULL);
    return 0; 

}

void *thread_function1(void *p)
{
    struct timeval tv;
    myindex = *(int *)p;
    gettimeofday(&tv, NULL);
    srand(tv.tv_sec + tv.tv_usec + getpid());
    printf("1\n");
    pthread_mutex_lock (&cv_m);
    while(cmd_ready == 0)
    {
        printf("2\n");
        pthread_cond_wait(&cv, &cv_m);
    }
    printf("3\n");

    throws[myindex] = rand() % 3;
    cmd_ready--;
    printf("Ready: %d\n",cmd_ready);
    pthread_mutex_unlock (&cv_m);
    printf("4\n");

    pthread_mutex_lock (&count_mutex);
    count++;
    printf("Count %d\n", count);
    pthread_mutex_unlock(&count_mutex);

    while(count == 2){
        printf("5\n");
        return NULL;
    }

}

char *guess_string(int g){
    switch(g){
    case 0:
        return "Rock";
        break;
    case 1:
        return "Paper";
        break;
    case 2:
        return "Scissors";
        break;
    }
}

int find_winner(int g1, int g2){
    if(g1 == g2)
        return -1; 
    else if ((g1 == 2) && (g2 == 0))
        return 1; 
    else if ((g1 == 0) && (g2 == 2))
        return 0;
    else
        return (g1 > g2) ? 0 : 1;
}
#包括
#包括
#包括
#包括
#包括
#定义第2行
结构时间值电视;
void*thread_function1();
void*thread_function2();
字符*猜测字符串(int g);
int胜[3];
int cmd_ready=0;
int x1=0,x2=1,计数=0;
int猜,对象,回合,i,j,k,l,winner,cmd,go,y;
pthread_mutex_t cv_m;
pthread_mutex_t count_mutex;
pthread_cond_t cv;
int-myindex;
int标志;
int抛出[3];
int main(int argc,char*argv[])
{
wins[0]=0;wins[1]=0;
如果((argc!=2)| |((turns=atoi(argv[1]))赢了[1])?1:2;
pthread_mutex_destroy(&cv_m);
pthread_cond_destroy(&cv);
pthread_exit(NULL);
返回0;
}
void*thread_函数1(void*p)
{
结构时间值电视;
myindex=*(int*)p;
gettimeofday(&tv,NULL);
srand(tv.tv_sec+tv.tv_usec+getpid());
printf(“1\n”);
pthread_mutex_lock(&cv_m);
while(cmd_ready==0)
{
printf(“2\n”);
pthread_cond_wait(&cv,&cv_m);
}
printf(“3\n”);
抛出[myindex]=rand()%3;
cmd_就绪--;
printf(“就绪:%d\n”,cmd\u就绪);
pthread_mutex_unlock(&cv_m);
printf(“4\n”);
pthread_mutex_lock(&count_mutex);
计数++;
printf(“计数%d\n”,计数);
pthread_mutex_unlock(&count_mutex);
而(计数=2){
printf(“5\n”);
返回NULL;
}
}
字符*guess_字符串(int g){
开关(g){
案例0:
返回“岩石”;
打破
案例1:
返回“文件”;
打破
案例2:
返回“剪刀”;
打破
}
}
int find_赢家(int g1,int g2){
如果(g1==g2)
返回-1;
else如果((g1==2)和&(g2==0))
返回1;
else如果((g1==0)和&(g2==2))
返回0;
其他的
返回(g1>g2)?0:1;
}

您似乎没有使用
pthread\u mutex\u init
pthread\u cond\u init
初始化互斥锁或条件

变量
myindex
正在由两个线程在没有保护的情况下修改,更新此变量的第二个线程将是显示为报告的线程

您还指望线程在main抓取锁并发出广播之前开始挂起,您可能会遇到main首先到达的情况,而您的线程不会准备好

这应该是一个开始