0

I'm trying to do a program in which there are two threads which talk like "pin-pong" way. The current result is the following:

the player 2 starts the game
1-I'm 139735290140224 and my ID is 2
2-I'm 139735290140224 and my ID is 2 and is turn of 1
3-I'm 139735290140224 and my ID is 2 and before the wait and turn is 1
1-I'm 139735298532928 and my ID is 1
2-I'm 139735298532928 and my ID is 1 and is turn of 2
3-I'm 139735298532928 and my ID is 1 and before the wait and turn is 2
4-I'm 139735290140224 and my ID is 2 and i'm after the wait and turn id 2

ID: 2, no boat ha drowned, coordinate(1,5)

and then it stops, while instead it should search in loop until one of the counter=10, so in this way:

the player 2 starts the game
1-I'm 139735298532928 and my ID is 2
2-I'm 139735298532928 and my ID is 2 and is turn of 1
3-I'm 139735290140224 and my ID is 1 and before the wait and turn is 2
4-I'm 139735290140224 and my ID is 1 and i'm after the wait and turn id 1
1-I'm 139735290140224 and my ID is 1
2-I'm 139735290140224 and my ID is 1 and is turn of 2
3-I'm 139735298532928 and my ID is 2 and before the wait and turn is 1
4-I'm 139735298532928 and my ID is 2 and i'm after the wait and turn id 2

..... and so on so far

Can somebody explain me how to use condition variables in a good and efficient way?

The program is this one:

void *battleship(void* index){
    int n_boat = 17;
    int ID_player=*(int*)index;
    char matrix[10][10];
    int matrix_drowned[10][10];
    int cnt_1=0;
            while(cnt<10){
            while(turn==ID_player){
                    pthread_mutex_lock(&playerMutex);
                    while (cnt>0)
                            pthread_cond_wait(&check, &playerMutex);
                    game_over=0;
                    printf("1-I'm %lu and my ID is %d\n", pthread_self(), ID_player);
                    if (turn==1)
                            turn=2;
                    else
                            turn=1;
                    printf("2-I'm %lu and my ID is %d and is turn of %d\n", pthread_self(), ID_player, turn);
                    pthread_cond_signal(&your_turn);
                    pthread_mutex_unlock(&playerMutex);
            }
            pthread_mutex_lock(&playerMutex);
            printf("3-I'm %lu and my ID is %d and before the wait and turn is %d\n", pthread_self(), ID_player, turn);
            while(turn!=ID_player)
                    pthread_cond_wait(&your_turn, &playerMutex);
            printf("4-I'm %lu and my ID is %d and i'm after the wait and turn id %d\n", pthread_self(), ID_player, turn);
            cnt++;
            game_over=1;
            pthread_cond_signal(&check);
            pthread_mutex_unlock(&playerMutex);
    }
    pthread_exit(NULL);

}

  • 1
    do you have a requirement to make this multi-threaded? Battleship is a linear game where each player takes turns. Multiple threads add needless complication without any benefit IMO. – yano May 26 '22 at 19:43
  • 1
    (1) The thread that does `pthread_cond_signal` should have the corresponding mutex _locked_. That mutex is what the other thread is doing `pthread_cond_wait` on. (2) Never call `pthread_cond_wait` without first checking the condition (e.g.) `while (! expr) pthread_cond_wait(cond,mutex);` – Craig Estey May 26 '22 at 19:44
  • What do u mean with check the condition? I'm sorry but I'm new in these things – lorenza_zazza May 26 '22 at 19:52
  • You [already] do: `while(turn!=ID) pthread_cond_wait(&your_turn, &playerMutex);` The expression in the `while` is the "condition" – Craig Estey May 26 '22 at 20:00
  • Clear, but why I have to do this? The conditional variable wait doesn't wait until it receives a signal? And what If thread 1 sends the signal while thread 2 isn't arrived yet to the wait condition? – lorenza_zazza May 26 '22 at 20:13
  • Assume `value` is 4. threadA will do: `lock(mutex); value = 5; pthread_cond_signal(cond); unlock(mutex);` Then, threadB does: `lock(mutex); while (value != 5) pthread_cond_wait(cond); /* do stuff */ unlock(mutex);` threadB sees that `value` is [already] 5, so it does _not_ call `pthread_cond_wait` – Craig Estey May 26 '22 at 22:30
  • I've modified the code, but still i got some issues... – lorenza_zazza May 27 '22 at 06:05

0 Answers0