logo
Course thumb

The C programming language made simple

Courses with this lesson

Difference between Binary Semaphores and Mutexes

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <semaphore.h> #define THREAD_NUM 4 pthread_mutex_t mutexFuel; sem_t semFuel; int fuel = 50; void* routine1(void* args) { while (1) { pthread_mutex_lock(&mutexFuel); fuel += 50; printf("Current value is %d\n", fuel); } } void* routine2(void* args) { while (1) { pthread_mutex_unlock(&mutexFuel); usleep(5000); } } int main(int argc, char *argv[]) { pthread_t th[THREAD_NUM]; sem_init(&semFuel, 0, 1); pthread_mutex_init(&mutexFuel, NULL); int i; for (i = 0; i < THREAD_NUM; i++) { if (i % 2 == 0) { if (pthread_create(&th[i], NULL, &routine1, NULL) != 0) { perror("Failed to create thread"); } } else { if (pthread_create(&th[i], NULL, &routine2, NULL) != 0) { perror("Failed to create thread"); } } } for (i = 0; i < THREAD_NUM; i++) { if (pthread_join(th[i], NULL) != 0) { perror("Failed to join thread"); } } pthread_mutex_destroy(&mutexFuel); sem_destroy(&semFuel); return 0; }