Reputation: 55
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]
Then, nothing happens.
What I want to happen (with the mutex being locked in appropriate places):
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.
Upvotes: 2
Views: 2366
Reputation: 11
Another note: you should have run these two lines before calling pthread_create();
There is no guarantee that your thread will be executed before these two lines.
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
Upvotes: 1
Reputation: 7164
You should lock the mutex before the wait and unlock it after wait is signaled, like this:
//...
while(!isGameOver()) {
pthread_mutex_lock(&mutex);
if(getCurrentPlayer() != PLAYER) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
// ...
See also here: https://computing.llnl.gov/tutorials/pthreads/
They have quite understandable examples there.
Upvotes: 1
Reputation: 254681
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:
while(!isGameOver()) {
if(!isValid(move)) {
move = findMove();
}
pthread_mutex_lock(&mutex); // LOCK HERE
if(getCurrentPlayer() != OPPONENT) {
pthread_cond_wait(&cond, &mutex);
}
if(getCurrentPlayer() == OPPONENT) {
if(isValid(move)) {
// NOT HERE pthread_mutex_lock(&mutex);
// ... update board with opponent's move ...
setCurrentPlayer(PLAYER);
pthread_cond_signal(&cond);
// NOT HERE pthread_mutex_unlock(&mutex);
}
}
pthread_mutex_unlock(&mutex); // UNLOCK HERE
}
and similarly for the other thread.
Upvotes: 3