-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemaphore.cpp
More file actions
35 lines (27 loc) · 806 Bytes
/
Copy pathsemaphore.cpp
File metadata and controls
35 lines (27 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t semaphore;
int counter = 0;
int thrd=0;
void* increment_counter(void* arg) {
sem_wait(&semaphore); // Decrease semaphore
thrd++;
for (int i = 0; i < 1000000; i++) {
counter++;
}
printf("Thread %d is finished with counter %d \n",thrd,counter);
sem_post(&semaphore); // Increase semaphore
return NULL;
}
int main() {
pthread_t thread1, thread2;
sem_init(&semaphore, 0, 1); // Initialize semaphore with value 1
pthread_create(&thread1, NULL, increment_counter, NULL);
pthread_create(&thread2, NULL, increment_counter, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
sem_destroy(&semaphore);
printf("Final counter: %d\n", counter);
return 0;
}