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 tosem_open("sem", O_CREAT|O_EXCL, S_IRWXU, 0)
sem_destory(&sem)
Change tosem_unlink("sem");
- And support
pthread_mutex_init(&mutex, NULL)
but not supportpthread_mutex_destory(&mutex)