Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从线程中检索和比较结果值?_C_Pthreads - Fatal编程技术网

C 如何从线程中检索和比较结果值?

C 如何从线程中检索和比较结果值?,c,pthreads,C,Pthreads,作为一项作业,我正在尝试用C语言做一个石头剪刀游戏,使用pthreads,重点关注障碍 它应该有用户定义的玩家数量和回合数。基本的石头剪刀规则接踵而至。对于每一个击败另一个的线程/玩家,它会为他添加一个分数,对于每一个平局,rng决定谁将获得分数(不考虑公平性,这主要是为了关注障碍)。最后,我将介绍一位获胜者 我想我成功地完成了线和障碍的事情,但是我想不出一个方法来完成得分的事情。我可以以一种有序的方式检索线程结果,但我不知道如何跟踪和比较n个玩家和结果 代码如下: #include <s

作为一项作业,我正在尝试用C语言做一个石头剪刀游戏,使用pthreads,重点关注障碍

它应该有用户定义的玩家数量和回合数。基本的石头剪刀规则接踵而至。对于每一个击败另一个的线程/玩家,它会为他添加一个分数,对于每一个平局,rng决定谁将获得分数(不考虑公平性,这主要是为了关注障碍)。最后,我将介绍一位获胜者

我想我成功地完成了线和障碍的事情,但是我想不出一个方法来完成得分的事情。我可以以一种有序的方式检索线程结果,但我不知道如何跟踪和比较n个玩家和结果

代码如下:

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

pthread_barrier_t barrier;

void* game(void *id) {
    int player = *(int*)id;
    int random = (int)(void*)(&player);
    int play = random % 3;
    if (play == 0) {
        printf("Player %d: played Rock \n", player);
    }
    if (play == 1) {
        printf("Player %d: played Paper \n", player);
    }
    if (play == 2) {
        printf("Player %d: played Scissors \n", player);
    }

    pthread_barrier_wait(&barrier);

    return (void *)play;
}


int main() {
    system("cls");
    int PLAYERS;
    printf("Players: \n");
    scanf("%d", &PLAYERS);
    printf("\n");
    int i, j;
    pthread_t id[PLAYERS];
    int num_id[PLAYERS];
    void *play;

    pthread_barrier_init(&barrier, NULL, PLAYERS + 1);

    for (i=0; i < PLAYERS; i++) {
        num_id[i] = i;
        pthread_create(&id[i], NULL, game, &num_id[i]);
    }

    pthread_barrier_wait(&barrier);

    for (i=0; i < PLAYERS; i++) {
        pthread_join(id[i], &play);
        printf("The hand of %d was %d\n", i, play); //printf debug
    }

    pthread_barrier_destroy(&barrier);

    return 0;
}
#包括
#包括
#包括
pthread_barrier_t barrier;
void*游戏(void*id){
int player=*(int*)id;
int random=(int)(void*)(&player);
int play=随机%3;
如果(播放==0){
printf(“玩家%d:玩过摇滚乐\n”,玩家);
}
如果(播放==1){
printf(“播放器%d:播放的纸张\n”,播放器);
}
如果(播放==2){
printf(“玩家%d:玩剪刀\n”,玩家);
}
pthread_barrier_wait(&barrier);
返回(无效*)播放;
}
int main(){
系统(“cls”);
国际球员;
printf(“玩家:\n”);
scanf(“%d”和玩家);
printf(“\n”);
int i,j;
pthread_t id[PLAYERS];
int num_id[玩家];
虚无*游戏;
pthread_barrier_init(&barrier,NULL,PLAYERS+1);
对于(i=0;i
仍然没有轮次,因为它仍然是在制品
最好的方法是什么?

来自:

从每个
game()
返回
play
变量值,然后将其保存到同一个变量中,只需将其保存到数组中即可

int main() {
    system("cls");
    int PLAYERS;
    printf("Players: \n");
    scanf("%d", &PLAYERS);
    printf("\n");
    int i, j;
    // using VLA from C99 isn't that much reliable, so i decided to use malloc here
    // pthread_t id[PLAYERS];
    pthread_t *id = malloc(sizeof(*id)*PLAYERS);
    // int num_id[PLAYERS];
    int *num_id = malloc(sizeof(*num_id)*PLAYERS);
    // void *play[PLAYERS];
    int *play = malloc(sizeof(*play)*PLAYERS);
    // maybe think of a structure here? 
    // struct player { pthread_t *id; int num_id; int play; } *players = malloc(sizeof(*players)*PLAYERS);

    pthread_barrier_init(&barrier, NULL, PLAYERS + 1);

    for (i=0; i < PLAYERS; i++) {
        num_id[i] = i;
        pthread_create(&id[i], NULL, game, &num_id[i]);
    }

    pthread_barrier_wait(&barrier);

    for (i=0; i < PLAYERS; i++) {
        pthread_join(id[i], (void*)&play[i]);
    }

    pthread_barrier_destroy(&barrier);

    for (i=0; i < PLAYERS; i++) {
        printf("The hand of %d was %d\n", i, play[i]);
    }
    // you want to compare first hand with second one?
    if (play[0] == play[1]) {
         printf("First two hands were the same.\n");
    }

    free(id);
    free(num_id);
    free(play);

    return 0;
}
intmain(){
系统(“cls”);
国际球员;
printf(“玩家:\n”);
scanf(“%d”和玩家);
printf(“\n”);
int i,j;
//使用C99中的VLA不太可靠,所以我决定在这里使用malloc
//pthread_t id[PLAYERS];
pthread_t*id=malloc(sizeof(*id)*玩家);
//int num_id[玩家];
int*num\u id=malloc(sizeof(*num\u id)*玩家);
//无效*玩[玩家];
int*play=malloc(sizeof(*play)*玩家);
//也许可以考虑一下这里的结构?
//结构播放器{pthread_t*id;int num_id;int play;}*players=malloc(sizeof(*players)*players);
pthread_barrier_init(&barrier,NULL,PLAYERS+1);
对于(i=0;i
但我认为更好的方法是将指向某个已定义结构的指针作为参数传递给线程,如下所示:

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

struct player_s {
  pthread_t *id;
  int num_id; 
  int play;
};

pthread_barrier_t barrier;

void* game(void *arg0) {
    struct player_s *p = arg0;
    assert(p != NULL);
    int player = p->num_id;
    int random = player; // this is not random ;)
    int play = random % 3;
    if (play == 0) {
        printf("Player %d: played Rock \n", player);
    }
    if (play == 1) {
        printf("Player %d: played Paper \n", player);
    }
    if (play == 2) {
        printf("Player %d: played Scissors \n", player);
    }

    pthread_barrier_wait(&barrier);

    p->play = play; // save play variable

    return NULL;
}


int main() {
    system("cls");
    int PLAYERS;
    printf("Players: \n");
    scanf("%d", &PLAYERS);
    printf("\n");
    int i, j;
    struct player_s *players = malloc(sizeof(*players)*PLAYERS);
    assert(players != NULL);

    pthread_barrier_init(&barrier, NULL, PLAYERS + 1);

    for (i=0; i < PLAYERS; i++) {
        players[i].num_id = i;
        pthread_create(&players[i].id, NULL, game, &players[i]);
    }

    pthread_barrier_wait(&barrier);

    for (i=0; i < PLAYERS; i++) {
        pthread_join(players[i].id, NULL);
    }

    pthread_barrier_destroy(&barrier);

    for (i=0; i < PLAYERS; i++) {
         printf("The hand of %d was %d\n", i, players[i].play);
    }
    // you want to compare first hand with second one?
    if (players[0].play == players[1].play) {
         printf("First two hands were the same.\n");
    }
    // compare first hand with all the others regardless of the array size
    for(i = 0; i < PLAYERS; ++i) {
        printf("The first hand was %d and the %d hand played %d, so they ", players[0].play, i, players[i].play);
        if (players[0].play != players[i].play) {
            printf("differ!\n");
        } else {
            printf("are the same!\n");
        }
    }
    free(players);

    return 0;
}
#包括
#包括
#包括
#包括
结构播放器{
pthread_t*id;
int num_id;
智力游戏;
};
pthread_barrier_t barrier;
void*游戏(void*arg0){
struct player_s*p=arg0;
断言(p!=NULL);
int player=p->num\u id;
int random=player;//这不是随机的;)
int play=随机%3;
如果(播放==0){
printf(“玩家%d:玩过摇滚乐\n”,玩家);
}
如果(播放==1){
printf(“播放器%d:播放的纸张\n”,播放器);
}
如果(播放==2){
printf(“玩家%d:玩剪刀\n”,玩家);
}
pthread_barrier_wait(&barrier);
p->play=play;//保存play变量
返回NULL;
}
int main(){
系统(“cls”);
国际球员;
printf(“玩家:\n”);
scanf(“%d”和玩家);
printf(“\n”);
int i,j;
struct player_*players=malloc(sizeof(*players)*players);
断言(玩家!=NULL);
pthread_barrier_init(&barrier,NULL,PLAYERS+1);
对于(i=0;i
如果我理解正确,您的问题与线程无关。(在allready使用join检索结果时也是错误的)。您的问题似乎只是您不知道如何评估未知数量的值,对吗?可能是的。我不知道如何存储这些值并相互比较。我知道如何在屏幕上打印它们,但我还不知道如何处理它们。第一个示例中的想法对我来说似乎是正确的,将其存储在数组中可以实现我猜的技巧,但我仍然不完全确定
int main() {
    system("cls");
    int PLAYERS;
    printf("Players: \n");
    scanf("%d", &PLAYERS);
    printf("\n");
    int i, j;
    // using VLA from C99 isn't that much reliable, so i decided to use malloc here
    // pthread_t id[PLAYERS];
    pthread_t *id = malloc(sizeof(*id)*PLAYERS);
    // int num_id[PLAYERS];
    int *num_id = malloc(sizeof(*num_id)*PLAYERS);
    // void *play[PLAYERS];
    int *play = malloc(sizeof(*play)*PLAYERS);
    // maybe think of a structure here? 
    // struct player { pthread_t *id; int num_id; int play; } *players = malloc(sizeof(*players)*PLAYERS);

    pthread_barrier_init(&barrier, NULL, PLAYERS + 1);

    for (i=0; i < PLAYERS; i++) {
        num_id[i] = i;
        pthread_create(&id[i], NULL, game, &num_id[i]);
    }

    pthread_barrier_wait(&barrier);

    for (i=0; i < PLAYERS; i++) {
        pthread_join(id[i], (void*)&play[i]);
    }

    pthread_barrier_destroy(&barrier);

    for (i=0; i < PLAYERS; i++) {
        printf("The hand of %d was %d\n", i, play[i]);
    }
    // you want to compare first hand with second one?
    if (play[0] == play[1]) {
         printf("First two hands were the same.\n");
    }

    free(id);
    free(num_id);
    free(play);

    return 0;
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct player_s {
  pthread_t *id;
  int num_id; 
  int play;
};

pthread_barrier_t barrier;

void* game(void *arg0) {
    struct player_s *p = arg0;
    assert(p != NULL);
    int player = p->num_id;
    int random = player; // this is not random ;)
    int play = random % 3;
    if (play == 0) {
        printf("Player %d: played Rock \n", player);
    }
    if (play == 1) {
        printf("Player %d: played Paper \n", player);
    }
    if (play == 2) {
        printf("Player %d: played Scissors \n", player);
    }

    pthread_barrier_wait(&barrier);

    p->play = play; // save play variable

    return NULL;
}


int main() {
    system("cls");
    int PLAYERS;
    printf("Players: \n");
    scanf("%d", &PLAYERS);
    printf("\n");
    int i, j;
    struct player_s *players = malloc(sizeof(*players)*PLAYERS);
    assert(players != NULL);

    pthread_barrier_init(&barrier, NULL, PLAYERS + 1);

    for (i=0; i < PLAYERS; i++) {
        players[i].num_id = i;
        pthread_create(&players[i].id, NULL, game, &players[i]);
    }

    pthread_barrier_wait(&barrier);

    for (i=0; i < PLAYERS; i++) {
        pthread_join(players[i].id, NULL);
    }

    pthread_barrier_destroy(&barrier);

    for (i=0; i < PLAYERS; i++) {
         printf("The hand of %d was %d\n", i, players[i].play);
    }
    // you want to compare first hand with second one?
    if (players[0].play == players[1].play) {
         printf("First two hands were the same.\n");
    }
    // compare first hand with all the others regardless of the array size
    for(i = 0; i < PLAYERS; ++i) {
        printf("The first hand was %d and the %d hand played %d, so they ", players[0].play, i, players[i].play);
        if (players[0].play != players[i].play) {
            printf("differ!\n");
        } else {
            printf("are the same!\n");
        }
    }
    free(players);

    return 0;
}