Skip to content

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_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

References


  1. Joinc_-_Pthread_API_Reference.pdf 

  2. POSIX_Multi_thread_programming.pdf