Skip to content

Instantly share code, notes, and snippets.

@NotRayor
Last active May 13, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotRayor/93d05da4c1b2401c4cddfc8fa7a0193e to your computer and use it in GitHub Desktop.
Save NotRayor/93d05da4c1b2401c4cddfc8fa7a0193e to your computer and use it in GitHub Desktop.
함수 단순 설명

pthread_create

pthread.h에 정의됨

함수 시그니쳐 : int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(start_routine)(void), void *restrict arg);

thread에 지정된 ID의 쓰레드를 특성정보를 지정하여 실행하고, 쓰레드가 수행할 함수와 함수의 매개변수를 정의하고 실행하는 함수이다.

성공시 0 , 실패시 0 이외의 값이 반환된다.

pthread_join

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_join(pthread_t thread, void **status);

thread에 지정된 ID의 쓰레드가 종료될 때까지 함수 호출한 프로세스(혹은 쓰레드)를 대기상태로 둔다.

status는 해당 쓰레드의 main 함수의 return 값을 받는 매개변수이다.

성공시 0 , 실패시 0 이외의 값이 반환된다.

pthread_mutex_init

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);

뮤텍스에 특성정보를 지정하고 생성하는 함수다. 생성된 뮤텍스는 mutex 포인터 변수에 참조된다.

성공시 0, 실패시 0 이외의 값이 반환된다.

pthread_mutex_destroy

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_mutex_destroy(pthread_mutex_t *mutex);

mutex 포인터 변수로 참조되는 뮤텍스를 제거한다.

성공시 0 , 실패시 0 이외의 값이 반환된다.

pthread_mutex_lock

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_mutex_lock(pthread_mutex_t *mutex);

임계영역에 들어가기 전에, 호출하여, 해당 쓰레드가 임계영역을 독점하도록 만든다.

성공 시 0, 실패 시 0 이외의 값이 반환된다.

pthread_mutex_unlock

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_mutex_unlock(pthread_mutex_t *mutex);

임계영역에서 작업을 마치고, 빠져나갈 때, 임계영역에 대한 독점을 해제한다.

성공 시 0, 실패 시 0 이외의 값이 반환된다.

pthread_detach

pthread.h 에 정의됨

함수 시그니쳐 : int pthread_detach(pthread_t thread);

대기 없이, 지정된 thread가 종료될 때 쓰레드를 소멸시키는 함수다.

성공 시 0, 실패 시 0 이외의 값 반환

주요 용어 정리

*restrict

restrict 포인터를 의미하며,각 포인터가 서로 다른 메모리 공간을 가리키고 있고, 다른 곳에서 접근하지 않으니 컴파일러가 최적화를 하라는 뜻이다.

세마포어

쓰레드 동기화를 할 수 있는 다른 방법, 세마포어의 대표적인 차이점은,

세마포어는 프로세스 단위로 적용할 수 있고, 임계영역에 여러 개의 프로세스가 접근할 수 있도록 선언할 수 있다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment