2
votes

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.

3
put some log statements around the critical sections and around the condition calls - you may see how the deadlock arises. At a guess, I'd say that the value of getCurrentPlayer() is not updating correctly...Nim
Try using error-checking mutexes. See PTHREAD_MUTEX_ERRORCHECK on pthread_mutexattr_settype.Maxim Egorushkin
I'm fairly sure the updated code should work. Can you show us getCurrentPlayer(), setCurrentPlayer(), and the code that initialises the value that they access?Mike Seymour

3 Answers

2
votes

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.

1
votes

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.

1
votes

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);