gpt4 book ai didi

c - 如何检索和比较线程的结果值?

转载 作者:行者123 更新时间:2023-11-30 16:32:18 25 4
gpt4 key购买 nike

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

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

e.g. 5 players, 2 rounds. They played: Rock(0), Rock(0), Scissors(2), Paper(1), Paper(1).

Round 1:

Player 1 tied with Player 2, beat Player 3 and lost to Player 4 and 5
Player 2 tied with Player 1, beat player 3 and lost to Player 4 and 5
Player 3 lost to Player 1 and 2 and beat Player 4 and 5
Player 4 beat Player 1 and 2, lost to Player 3 and tied with Player 5
Player 5 beat Player 1 and 2, lost to Player 3 and tied with Player 4

End score would be:
Player 1: 2 (RNG has chosen him)
Player 2: 1
Player 3: 2
Player 4: 2
Player 5: 3 (RNG has chosen him)

Round 2

...

Player 1: 4 (round 1: 2 + round 2: 2)
Player 2: 2 (1+1)
Player 3: 5 (2+3)
Player 4: 4 (2+2)
Player 5: 5 (3+2)

Winner: Player 3 (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;
}

仍然没有回合,因为它仍然是 WIP
最好的方法是什么?

最佳答案

来自man pthread_join :

int pthread_join(pthread_t thread, void **retval);
If retval is not NULL, then pthread_join() copies the exit status of
the target thread (i.e., the value that the target thread supplied to
pthread_exit(3)) into the location pointed to by retval

您从每个 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;
}

但我认为更好的方法是将指向某个已定义结构的指针作为线程的参数传递,如下所示:

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

关于c - 如何检索和比较线程的结果值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com