POSIX Threads
lastmod 2022-10-07created 2016-02-1815 edits
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 means "to separate", and it detaches a thread created with pthread_create from the main thread. This function detaches the thread with identifier th; once detached, when that (detached) thread terminates, all its resources are freed immediately even without calling pthread_join.
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_*
If pthread-related symbols cannot be found as shown below, add -lpthread or adjust its position.
/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
You may get a warning like the following.
threads_pthread.c:(.text+0x1ba): undefined reference to `pthread_atfork'
collect2: error: ld returned 1 exit status
If you used -lpthread, change it to -pthread.
See also
Favorite site
- Wikipedia (en) POSIX Threads
- GNU Hurd/ POSIX Threading Library
- pthread win32 web site
- When pthread cannot be found in MinGW (gcc)
- Pthread API Reference (ko) 1
- Thread Programming in a UNIX environment
- Multithreaded programming with POSIX threads 2
- Pthread(3): Going deeper...
- Summary of pthread functions
- About pthread....
- Explanation of pthread
- [Recommended] On releasing pthread resources (pthread_detach, pthread_attr_setdetachstate)
- Man Page/pthread_attr_init.3