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

typedef struct {
    sem_t sem;
    pthread_mutex_t mutex;
    intフラグ;
} cond_var_t;

void cond_var_init（cond_var_t* cv）{
    sem_init（＆cv-> sem、0、0）;
    pthread_mutex_init（＆cv-> mutex、null）;
    cv-> flag = 0;
}

void cond_var_signal（cond_var_t* cv）{
    pthread_mutex_lock（＆cv-> mutex）;
    cv-> flag = 1;
    sem_post（＆cv-> sem）;
    pthread_mutex_unlock（＆cv-> mutex）;
}

void cond_var_wait（cond_var_t* cv）{
    pthread_mutex_lock（＆cv-> mutex）;
    while（cv-> flag == 0）{
        pthread_mutex_unlock（＆cv-> mutex）;
        sem_wait（＆cv-> sem）;
        pthread_mutex_lock（＆cv-> mutex）;
    }
    cv-> flag = 0;
    pthread_mutex_unlock（＆cv-> mutex）;
}

void cond_var_destroy（cond_var_t* cv）{
    sem_destroy（＆cv-> sem）;
    pthread_mutex_destroy（＆cv-> mutex）;
}
#include <pthread.h>
#include <semaphore.h>

typedef struct {
    sem_t sem;
    pthread_mutex_t mutex;
    int flag;
} cond_var_t;

void cond_var_init(cond_var_t* cv) {
    sem_init(&cv->sem, 0, 0);
    pthread_mutex_init(&cv->mutex, NULL);
    cv->flag = 0;
}

void cond_var_signal(cond_var_t* cv) {
    pthread_mutex_lock(&cv->mutex);
    cv->flag = 1;
    sem_post(&cv->sem);
    pthread_mutex_unlock(&cv->mutex);
}

void cond_var_wait(cond_var_t* cv) {
    pthread_mutex_lock(&cv->mutex);
    while (cv->flag == 0) {
        pthread_mutex_unlock(&cv->mutex);
        sem_wait(&cv->sem);
        pthread_mutex_lock(&cv->mutex);
    }
    cv->flag = 0;
    pthread_mutex_unlock(&cv->mutex);
}

void cond_var_destroy(cond_var_t* cv) {
    sem_destroy(&cv->sem);
    pthread_mutex_destroy(&cv->mutex);
}