int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
tid를 변수에 저장하고 return 0, if fail return 에러코드 값
➕ Parameter
thread
: tid 저장하는 변수
attr
: NULL = default
start_routine
: functuin pointer ➡️ function name
= 시작 address를 저장하기 위한 pointer (th는 이 함수부터 시작)
arg
: argument ( parameter )
pthread_create(&tid, NULL, runner, argv[1]);
int pthread_attr_init(pthread_attr_t *attr);
return 0, if fail return # of error
pthread_attr_init(&attr);
int pthread_join(pthread_t th, void **thread_return);
return 0, if fail return # of error
pthread_join(tid, NULL);
: Terminates thread
void pthread_exit(void *retval);
➕ Parameter
retval
: 같은 프로세스 내의 다른 스레드가 pthread_join을 통해서 받기 가능
pthread_exit(0);
: Terminating a thread before it has completed
int pthread_cancel(pthread_t thread);
return 0, if fail return # of error
→ 바로 죽이는 명령 x
int pthread_setcanceltype(int type, int *oldtype);
return 0, if fail return # of error
➕ Parameter
type
: cancellation type
- PTHREAD_CANCEL_ASYNCHRONOUS : 즉시 종료
- PTHREAD_CANCEL_DEFERRED : pthread_testcancel()에서 종료
➡️ 안전한 곳에만 둬야함, Safer than asynchronous cancellation
oldtype
: 이전 type을 oldtype가 가리키는 버퍼로 반환
pthread_t pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
int pthread_setcancelstate(int state, int *oldstate);
return 0, if fail return # of error
➕ Parameter
state
: cancellation state
- PTHREAD_CANCEL_ENABLE
- PTHREAD_CANCEL_DISABLE : cancellation remains pending until thread enables it
pthread_t pthread_setcanceltype(PTHREAD_CANCEL_ENABLE, &oldstate);
__thread int tls
: Each thread has its own ‘int tls’ variable
pthread_attr_setscope(pthread_attr_t *attr, int scope);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_create(&tid, &attr, runner, NULL);
pthread_attr_getscope(pthread_attr_t *attr, int *scope);
int scope = 0;
pthread_attr_getscope(&attr, &scope);
pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
return 0, if fail return # of error
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
fprinf(stderr, "Unable to set policy\n");
for(i=0; i<NUM_THREAD; i++)
pthread_create(&tid[i], &attr, runner, NULL);
pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
return 0, if fail return # of error
int policy = 0;
if(pthread_attr_getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy\n");