程式語言 - GNU - C/C++ - PThread Wait On Condition



參考資訊:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html
https://stackoverflow.com/questions/16522858/understanding-of-pthread-cond-wait-and-pthread-cond-signal

main.c

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

pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 

void* thread_handler(void *arg) 
{
    pthread_mutex_lock(&lock); 
    pthread_cond_wait(&cond, &lock); 
    pthread_mutex_unlock(&lock); 
    return NULL; 
}    

int main(int argc, char *argv[]) 
{ 
    pthread_t tid = { 0 };

    pthread_create(&tid, NULL, thread_handler, NULL);

    sleep(1); 
    pthread_cond_signal(&cond); 
    pthread_join(tid, NULL); 
    return 0; 
}