Different behaviors between Linux and Windows using threads(pthreads)

Asked

Viewed 169 times

1

I’m using the Linux pthreads library to try out the functionality of threads, the code below prints 5 messages on the screen for each thread, each thread waits its turn to display the message controlled using the semaphores, on Windows works perfectly, but in linux threads do not wait their turn, I already researched everything that is singing and could not find a solution to this problem.

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

typedef struct param{
  int id;
  pthread_mutex_t lock;
  sem_t semaforo;
}valores_t;

void * olamundo(void* args){
  valores_t* p = args;
  sem_post(&p->semaforo);
  for (size_t i = 0; i < 5; i++) {
    printf("Ola mundo da thread %d\n", p->id);
  }
  sem_wait(&p->semaforo);
}

sem_t semaforo;

int main(int argc, char const *argv[]) {
  /* code */

  if(sem_init(&semaforo,0,1)){//valor inicial do semaforo começa por 1
    printf("Erro ao iniciar o semaforo\n");
  }

  valores_t p[2];
  pthread_t threads[2];

    p[0].id = 1;
    p[0].semaforo = semaforo;

    p[1].id = 2;
    p[1].semaforo = semaforo;

  for(int i = 0; i < 2; i++){//inicia as funcoes das threads
    if(pthread_create(&(threads[i]), NULL, &olamundo, &p[i]) == -1){
      printf("Erro ao inicializar a thread\n");
    }
  }

  for(int i = 0; i < 2; i++){
        if(pthread_join(threads[i], NULL)){
      printf("Erro ao sincronizar a thread\n");
    }
    }
    sem_destroy (&semaforo);
  return 0;
}
  • https://stackoverflow.com/questions/30434745/c-11-threads-different-behaviour-on-linux-and-windows

  • 1

    i was able to fix this problem in the other post I did on stackoverflow https://stackoverflow.com/questions/49503594/using-parameters-with-threads

  • the error is obvious (and was pointed out in the other OS): you are reversing the order fas operations sem_wait and sem_post...

1 answer

-1


It turns out that it is a bug in the compiler, to solve this problem to work on both platforms use a pointer in struct, for more details on the solution see this other thread in stackoverflow threads

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

typedef struct param{
    int id;
    pthread_mutex_t lock;
    sem_t *semaforo; //Dagan: change to a pointer to a semaphore
}valores_t;

void * olamundo(void* args){
    valores_t* p = args;
    sem_wait(*(&p->semaforo)); //Dagan: use the semaphore pointer
    for (size_t i = 0; i < 25; i++) {
        printf("Ola mundo da thread %d\n", p->id);
    }
    sem_post(*(&p->semaforo)); //Dagan: use the semaphore pointer
}

sem_t semaforo;

int main(int argc, char const *argv[]) {
    /* code */

    if(sem_init(&semaforo,0,1)){//valor inicial do semaforo começa por 1
        printf("Erro ao iniciar o semaforo\n");
    }

    valores_t p[2];
    pthread_t threads[2];

    p[0].id = 1;
    p[0].semaforo = &semaforo; //Dagan: pass the address of the semaphore

    p[1].id = 2;
    p[1].semaforo = &semaforo; //Dagan: pass the address of the semaphore

    for(int i = 0; i < 2; i++){//inicia as funcoes das threads
        if(pthread_create(&(threads[i]), NULL, &olamundo, &p[i]) == -1){
            printf("Erro ao inicializar a thread\n");
        }
    }

    for(int i = 0; i < 2; i++){
        if(pthread_join(threads[i], NULL)){
            printf("Erro ao sincronizar a thread\n");
        }
    }
    sem_destroy (&semaforo);
    return 0;
}
  • 2

    as I commented above, the error is the order of use of the operations at the semaphore...Anyway, if you have a correct solution you should put here, and not point to a question in the OS in English

  • I guess you didn’t understand my question, the code I have works on linux, because the order of the semaphores would make it not work in windows? Anyway I had a solution and explained above, it would be redundant to put the code with the solution since it is in my original thread in English

  • well, I work with threads daily, and this has been many years, I think it is much more likely that you have not been able to explain than I have not understood...as to the "answer" you had, it would not be "redundande" put here, after all you asked the question here...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.