POSIX Threads
The POSIX 1003.1-2001 standard defines an application programming interface (API) for writing multithreaded applications. This interface is known more commonly as pthreads. A good number of modern operating systems include a threading library of some kind: Solaris (UI) threads, Win32 threads, DCE threads, DECthreads, or any of the draft revisions of the pthreads standard. The trend is that most of these systems are slowly adopting the pthreads standard API, with application developers following suit to reduce porting woes.
APIs
- pthread_create
- pthread_join
- pthread_exit
- pthread_attr_init
- pthread_cancel
- pthread_kill
- pthread_cleanup_push
- pthread_cleanup_pop
- pthread_mutex_init
PTHREAD_CREATE_DETACHED
detach 는 "떼어내다" 라는 뜻을 가지며 main 쓰레드에서 pthread_create 를 이용해 생성된 쓰레드를 분리시킨다. 이 함수는 식별번호th인 쓰레드를 detach 시키는데, detach 되었을경우 해당(detach 된) 쓰레드가 종료될경우 pthread_join 을 호출하지 않더라도 즉시 모든 자원이 해제(free) 된다.
PTHREAD_CREATE_JOINABLE
pthreadGC2 vs pthreadGCE2
먼저 pthreadGC2 라이브러리를 만들고 pthreadGCE2 라이브러리를 만들어야 한다.
GCE는 C++ 예외 처리가 추가된 것이다. pthreadGC2 라이브러리를 빌드하지 않은 상태에서 바로 pthreadGCE2 라이브러리를 만들 수 없다.
자신이 C++을 사용한다고 하더라도 GC 버전은 먼저 빌드해야 한다.
makefile이 있고, GNUmakefile이 있기 때문에 make -f GNUmakefile을 사용하거나, 그냥 make를 입력하면 도움말이 나오니 필요한 옵션을 선택해서 빌드하면 된다.
Troubleshooting
undefined reference to symbol pthread_*
아래와 같이 pthread관련 심볼을 찾을 수 없다면 -lpthread
를 추가하거나 위치를 조정해 주면 된다.
/usr/bin/ld: ./libworld.a(Log.world.o): undefined reference to symbol 'pthread_rwlock_wrlock@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
undefined reference to pthread_atfork
아래와 같은 경고가 발생될 수 있다.
threads_pthread.c:(.text+0x1ba): undefined reference to `pthread_atfork'
collect2: error: ld returned 1 exit status
만약 -lpthread
를 사용했다면, -pthread
로 변경하자.
See also
Favorite site
- Wikipedia (en) POSIX Threads
- GNU Hurd/ POSIX Threading Library
- pthread win32 web site
- MinGW(gcc) 에서 pthread 를 찾을 수 없을때
- Pthread API Reference (ko) 1
- UNIX 환경하에서 Thread Programming
- POSIX 쓰레드로 멀티 쓰레드 프로그래밍하기 2
- Pthread(3): 더 깊이...
- pthread 함수 요약
- pthread에 관해....
- pthread에 관현된 설명
- [추천] pthread 자원 해제에 대한 이야기(pthread_detach, pthread_attr_setdetachstate)
- Man Page/pthread_attr_init.3