I’m making a game where the user plays against the computer. The computer opponent thinks about its next move while it is the player’s turn. If the player moves to the spot where the computer opponent was planning to move, the computer opponent starts its search for its move over again.
Here’s an outline of the main function and the opponent function:
[UPDATED]
pthread_mutex_t mutex;
pthread_cond_t cond;
int main() {
// ... initialize game variables, args to pass to opponent ...
pthread_t thread;
pthread_create(&thread, NULL, opponent, &args);
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
while(!isGameOver()) {
pthread_mutex_lock(&mutex);
if(getCurrentPlayer() != PLAYER) {
pthread_cond_wait(&cond, &mutex);
}
if(getCurrentPlayer() == PLAYER) {
// ... update board with player's move ...
setCurrentPlayer(OPPONENT);
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
}
}
void * opponent(void * args) {
// initialize move to something invalid
while(!isGameOver()) {
if(!isValid(move)) {
move = findMove();
}
pthread_mutex_lock(&mutex);
if(getCurrentPlayer() != OPPONENT) {
pthread_cond_wait(&cond, &mutex);
}
if(getCurrentPlayer() == OPPONENT) {
if(isValid(move)) {
// ... update board with opponent's move ...
setCurrentPlayer(PLAYER);
pthread_cond_signal(&cond);
}
}
pthread_mutex_unlock(&mutex);
}
}
Currently, it seems like this is what is happening: [UPDATED]
- the opponent finds his move (findMove)
- the opponent locks the mutex (pthread_mutex_lock)
- the opponent starts waiting (pthread_cond_wait)
- the main function locks the mutex (pthread_mutex_lock)
- the player makes his move
- the main thread signals that it is the opponents turn (pthread_cond_signal)
Then, nothing happens.
What I want to happen (with the mutex being locked in appropriate places):
- the opponent finds his move (findMove)
- the opponent starts waiting (pthread_cond_wait)
- the player makes his move
- the main thread signals that it is the opponents turn (pthread_cond_signal)
- the opponent stops waiting
- the opponent makes the move it was previously thinking about
- the opponent switches the current player (setCurrentPlayer)
- repeat
I’m not really experienced with threads, so could someone help me out with what is going on here, and how I could possibly fix it? I don’t know if I have the mutex lock/unlock, condition signal/wait, and setCurrentPlayer functions in the right places.
The mutex should be locked when you call
pthread_cond_wait– that function atomically unlocks the mutex, waits for a signal, and relocks the mutex before returning. The purpose of the mutex is to serialise access to the shared state (in this case the current player), so it should be locked around any access to that. The loop should be more like:and similarly for the other thread.