Thead 분류
- User-level thread : 프로세스 차원에서 쓰레드 관리 (효율적)
- Kernel-level thread : 커널이 쓰레드 관리(복잡하면 이거 씀)
Thread 지원
- 대표적인 라이브러리
- pthread, windos thread, java thread
Pthread (Posix thread)
- 표준 thread API 제정
- 60개가 넘은 함수로 수정
- #include <pthread.h>
Pthread
함수 분류
- 쓰레드 관리 : pthread_, pthread_attr_
- 상호 배제 : pthread_mutex_, pthread_mutexattr_
- 쓰레드 간 동기화, 공유 변수 관리에 사용
- Mutex 영역 생성, 소멸, lock/unlock
- Mutex 관련 속성의 설정 또는 변경
- 조건 변수 : pthread_cond_, pthread_condattr_
- mutex를 공유하는 쓰레드 간 동기화에 사용
쓰레드 생성 & 종료
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
void pthread_exit(void *retval);
- thread : 생성이 성공할 경우 생성된 쓰레드의 id
- attr : 속성 객체, 기본 속성을 사용하려면 NULL로 설정
- start_routine : 쓰레드로 분기하여 실행할 함수