Skip to content
Snippets Groups Projects
Commit cc15cb60 authored by Mactavish's avatar Mactavish
Browse files

add semaphore solution

parent 883299a0
No related branches found
No related tags found
No related merge requests found
#include "semaphore.h"
void sem_init(semaphore *sem, int count) {
// TODO: implement this function
sem->count = count;
pthread_mutex_init(&sem->mutex, NULL);
pthread_cond_init(&sem->condition, NULL);
}
void sem_wait(semaphore *sem) {
// TODO: implement this function
pthread_mutex_lock(&sem->mutex);
while (sem->count <= 0) {
pthread_cond_wait(&sem->condition, &sem->mutex);
}
sem->count--;
pthread_mutex_unlock(&sem->mutex);
}
void sem_post(semaphore *sem) {
// TODO: implement this function
pthread_mutex_lock(&sem->mutex);
sem->count++;
pthread_cond_signal(&sem->condition);
pthread_mutex_unlock(&sem->mutex);
}
void sem_destroy(semaphore *sem) {
// TODO: implement this function
pthread_mutex_destroy(&sem->mutex);
pthread_cond_destroy(&sem->condition);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment