The difference between linux semaphore sem_t and pthread_cond_t

 

Try to use sem instead of pthread_cond 

Because pthread_cond will have the problem of losing the signal, sem is an atomic operation, so it will not lose the signal

 

Sem_init()/sem_destory() is not supported under Mac:

note:

MacOS does not support  sem_init() and  sem_destroy(); if you want to compile this example under mac, you need to modify and replace related functions yourself.

  • sem_init(&sem, 0, 1) Change to sem_open("sem", O_CREAT|O_EXCL, S_IRWXU, 0)
  • sem_destory(&sem) Change to sem_unlink("sem");
  • And support  pthread_mutex_init(&mutex, NULL) but not support pthread_mutex_destory(&mutex)

 

Guess you like

Origin blog.csdn.net/star871016/article/details/109688578