0

I've written a game to move "players" around a board using a semaphore to lock the board before allowing another player access.

I'll skip most of the code for the sake of brevity, but here are the things I'm getting messed up with.

The first is a function that runs the thread for one of the "players"

void* xTurn(){
int move;    //generate random number to place items on board
move = rand()%4+1;
//generates a number between 1 and 4
while(tokens!=0){ //while there are still "tokens" on the board, continue
    sem_wait(&sB);
    for(int i = 0; i < ROW; i++){
        for(int j = 0; j < COL; j++){
            if(board[i][j] == 'X'){
                switch(move){
                    case '1':
                        if(i++>8){
                            xTurn();
                        }
                        else{
                            if(board[i++][j]=='a'){
                                xScore++;
                                tokens--;
                            }
                            if(board[i++][j]=='A'){
                            xScore+2;
                            tokens--;
                            }
                        board[i][j]='-';
                        board[i++][j]='X';
                        break;
                        }
                    case '2':
                        if(i--<0){
                            xTurn();
                        }
                        else{
                            if(board[i--][j]=='a'){
                                xScore++;
                                tokens--;
                            }
                            if(board[i--][j]=='A'){
                                xScore+2;
                                tokens--;
                            }
                        board[i][j]='-';
                        board[i--][j]='X';
                        break;
                        }
                    case '3':
                        if(j++>8){
                            xTurn();
                        }
                        else{
                            if(board[i][j++]=='a'){
                                xScore++;
                                tokens--;
                            }
                            if(board[i][j++]=='A'){
                                xScore+2;
                                tokens--;
                            }
                        board[i][j]='-';
                        board[i][j++]='X';
                        break;
                        }
                    case '4':
                        if(j--<0){
                            xTurn();
                        }
                        else{
                            if(board[i][j--]=='a'){
                                xScore++;
                                tokens--;
                            }
                            if(board[i][j--]=='A'){
                                xScore+2;
                                tokens--;
                            }
                        board[i][j]='-';
                        board[i][j--]='X';
                        break;
                    }
                }
            }
        }
    }
}
sem_post(&sB);
}

And I'm calling it here. Just assume that I have methods yTurn and zTurn; print is headed in a similar manner.

void playgame(){
createBoard();
srand (time(NULL));

sem_init(&sB,0,0);
pthread_create(&tX,NULL,&xTurn,NULL);
pthread_create(&tY,NULL,&yTurn,NULL);
pthread_create(&tZ,NULL,&zTurn,NULL);
pthread_create(&tP,NULL,&print,NULL);
pthread_join(tX,NULL);
pthread_join(tY,NULL);
pthread_join(tZ,NULL);
pthread_join(tP,NULL);


if(xScore>yScore&&zScore){
cout<<"Player X Wins with a score of "<<xScore;
}
if(yScore>xScore&&zScore){
cout<<"Player Y Wins with a score of "<<yScore;
}
if(zScore>yScore&&xScore){
cout<<"Player Z Wins with a score of "<<zScore;
}
sleep(20);
menu();
}

When I run it I get two different errors:

One that tells me sleep is not declared, but that will be solved when running it Linux. Two is --invalid conversion from 'void* ()()' to 'void(*)(void*_' [-fpermissive] This problem occures at the third argument in the pthread_create. I haven't a clue as to what that means. I've tried a number of different things, but haven't the slightest idea as to how to solve the problem

4

2 回答 2

4

pthread_create 的签名是:

 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

所以你传递给它的函数必须接受一个 void* 参数,即使你没有使用它,即你的声明应该是

void* xTurn(void*) ...
于 2013-05-01T23:46:00.840 回答
2

by 调用的线程函数pthread_create应该有一个void *参数并返回一个void *值。

所以,改变:

 void* xTurn()

 void* xTurn(void *)
于 2013-05-01T23:46:10.590 回答