Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 컴퓨터공학과
- coding
- 정석학술정보관
- 컴공과
- 오퍼레이팅시스템
- Operating System
- 북리뷰
- 자료구조
- 개발
- 너비우선탐색
- 알고리즘
- 문제풀이
- 백준
- 정석
- 오에스
- cs
- 스택
- 브루트포스
- OS
- Computer science
- 그래프
- 코딩
- vector
- bfs
- 코테
- c++
- DP
- 구현
- 컴공
- Stack
Archives
- Today
- Total
Little Jay
[OS] pthread.h 실행 명령어 본문
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ITER 1000
void *thread_increment(void * arg);
void *thread_decrement(void * arg);
int x;
sem_t s;
int main() {
pthread_t tid1, tid2;
sem_init(&s, 0, 1);
pthread_create(&tid1, NULL, thread_increment, NULL);
pthread_create(&tid2, NULL, thread_decrement, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
if (x != 0 ) printf("boom! counter=%d\n", x);
else printf("ok counter=%d\n", x);
sem_destroy(&s);
}
void * thread_increment(void *arg) {
int i, val;
for (i = 0; i < ITER; i++) {
sem_wait(&s);
val = x;
printf("%u: %d\n", (unsigned int) pthread_self(), val);
x = val + 1;
sem_post(&s);
}
return NULL;
}
void * thread_decrement(void *arg) {
int i, val;
for (i = 0; i < ITER; i++) {
sem_wait(&s);
val = x;
printf("%u: %d\n", (unsigned int) pthread_self(), val);
x = val - 1;
sem_post(&s);
}
return NULL;
}
gcc thread.c -lpthread 해줘야한다.
'Univ > Study' 카테고리의 다른 글
KMOOC X Coursera 수강권 이벤트 (0) | 2023.01.16 |
---|---|
[Study] 코드 표절검사 by Stanford Moss (0) | 2022.05.18 |
객체 지향의 원칙들(SOLID) (0) | 2022.03.04 |
[C++] for 문을 사용할때 조건식에서 주의해야 할 점(unsigned와 size_type 그리고 overflow) (0) | 2022.02.17 |
[C++] emplace_back()과 push_back(), 그리고 Clang-Tidy Bug (0) | 2022.02.16 |
Comments