-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsem.cpp
More file actions
35 lines (30 loc) · 966 Bytes
/
Copy pathsem.cpp
File metadata and controls
35 lines (30 loc) · 966 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 <semaphore.h>
#include <iostream>
#include <thread>
int sharedData = 0; // 공유 데이터
sem_t semaphore; // 세마포어 선언
void increment() {
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore); // 세마포어 획득
sharedData++; // 공유 데이터 증가
sem_post(&semaphore); // 세마포어 해제
}
}
void decrement() {
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore); // 세마포어 획득
sharedData--; // 공유 데이터 감소
sem_post(&semaphore); // 세마포어 해제
}
}
int main() {
sem_init(&semaphore, 0, 1); // 세마포어 초기화, 공유 자원 1개
std::thread thread1(increment);
std::thread thread2(decrement);
thread1.join();
thread2.join();
std::cout << "Final value of sharedData: " << sharedData << std::endl;
// 세마포어 제거
sem_destroy(&semaphore);
return 0;
}