【原创】多线程应用中pthread库使用问题
生活随笔
收集整理的這篇文章主要介紹了
【原创】多线程应用中pthread库使用问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
在 linux 下開發多線程應用,大多情況下我們都會使用 pthread (即 POSIX thread)這個庫。該庫遵循 POSIX.1-2001 標準。
在使用該庫的過程中,肯定有人見過各種 gcc 選項配置方式:
- -pthread
- -pthreads
- -lpthread
在 GCC man 手冊中,我們可以找到如下內容
... Machine Dependent Options ... HPPA Options ...-threadsAdd support for multithreading with the dce thread library under HP-UX. This option sets flags for both the preprocessor and linker. ... IA-64 Options ...-pthreadAdd support for multithreading using the POSIX threads library. This option sets flags for both the preprocessor and linker. It does not affect the thread safety of object code produced by the compiler or that of libraries supplied with it. These are HP-UX specific flags. ... IBM RS/6000 and PowerPC Options ...-pthreadAdds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker. ... SPARC Options ...-threadsAdd support for multithreading using the Solaris threads library. This option sets flags for both the preprocessor and linker. This option does not affect the thread safety of object code produced by the compiler or that of libraries supplied with it.-pthreadsAdd support for multithreading using the POSIX threads library. This option sets flags for both the preprocessor and linker. This option does not affect the thread safety of object code produced by the compiler or that of libraries supplied with it.-pthreadThis is a synonym for -pthreads. ... 可以看到,這幾個選項都是 Machine Dependent 的。而什么是 Machine Dependent ,則可以參考 這里 。
還有以下內容
... Options for Linking ... -llibrary-l librarySearch the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o-lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specifiedprecisely by name.The directories searched include several standard system directories plus any that you specify with -L.Normally the files found this way are library files---archive files whose members are object files. The linker handles an archive file by scanning through it for memberswhich define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The onlydifference between using an -l option and specifying a file name is that -l surrounds library with lib and .a and searches several directories. 從 GCC 選項分類上,上面兩種選項分別屬于
GCC Command Options -> Hardware Models and Configurations -> xxx Options
GCC Command Options -> Options for Linking
而 -pthread 和 -pthreads 的關系可以簡單認為,是在某一種 Hardware Model 上支持的同義選項。
至于為何推薦使用 -pthread 選項,可以參考下圖中的說明
下面實驗驗證一下,幾種方式的效果
[root@Betty thread_test]# uname -a Linux Betty 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux [root@Betty thread_test]# gcc -V gcc: ‘-V’選項必須有一個參數 [root@Betty thread_test]# gcc -v 使用內建 specs。 目標:x86_64-redhat-linux 配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux 線程模型:posix gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) [root@Betty thread_test]# [root@Betty thread_test]# [root@Betty thread_test]# ll 總用量 5320 -rw-r--r-- 1 root root 45125 8月 20 09:24 event.h lrwxrwxrwx 1 root root 21 8月 19 15:02 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9 -rwxr-xr-x 1 root root 968698 8月 19 15:02 libevent-2.0.so.5.1.9 -rw-r--r-- 1 root root 1571978 8月 19 15:02 libevent.a lrwxrwxrwx 1 root root 26 8月 19 15:02 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9 -rwxr-xr-x 1 root root 585233 8月 19 15:02 libevent_core-2.0.so.5.1.9 -rw-r--r-- 1 root root 978626 8月 19 15:02 libevent_core.a lrwxrwxrwx 1 root root 26 8月 19 15:02 libevent_core.so -> libevent_core-2.0.so.5.1.9 lrwxrwxrwx 1 root root 27 8月 19 15:02 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9 -rwxr-xr-x 1 root root 404860 8月 19 15:02 libevent_extra-2.0.so.5.1.9 -rw-r--r-- 1 root root 593424 8月 19 15:02 libevent_extra.a lrwxrwxrwx 1 root root 27 8月 19 15:02 libevent_extra.so -> libevent_extra-2.0.so.5.1.9 lrwxrwxrwx 1 root root 29 8月 19 15:02 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.9 -rwxr-xr-x 1 root root 94281 8月 19 15:02 libevent_openssl-2.0.so.5.1.9 -rw-r--r-- 1 root root 131932 8月 19 15:02 libevent_openssl.a lrwxrwxrwx 1 root root 29 8月 19 15:02 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.9 lrwxrwxrwx 1 root root 30 8月 19 15:02 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9 -rwxr-xr-x 1 root root 18438 8月 19 15:02 libevent_pthreads-2.0.so.5.1.9 -rw-r--r-- 1 root root 18678 8月 19 15:02 libevent_pthreads.a lrwxrwxrwx 1 root root 30 8月 19 15:02 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9 lrwxrwxrwx 1 root root 21 8月 19 15:02 libevent.so -> libevent-2.0.so.5.1.9 -rw-r--r-- 1 root root 9458 8月 20 09:24 thread.h -rw-r--r-- 1 root root 757 8月 20 10:22 thread_test.c [root@Betty thread_test]# [root@Betty thread_test]# [root@Betty thread_test]# gcc thread_test.c -o thread_test_1 -levent_core -levent_pthreads -lpthread [root@Betty thread_test]# gcc thread_test.c -o thread_test_2 -levent_core -levent_pthreads -pthread [root@Betty thread_test]# gcc thread_test.c -o thread_test_3 -levent_core -levent_pthreads -pthreads gcc: 無法識別的選項‘-pthreads’ [root@Betty thread_test]# [root@Betty thread_test]# ll 總用量 5344 -rw-r--r-- 1 root root 45125 8月 20 09:24 event.h lrwxrwxrwx 1 root root 21 8月 19 15:02 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9 -rwxr-xr-x 1 root root 968698 8月 19 15:02 libevent-2.0.so.5.1.9 -rw-r--r-- 1 root root 1571978 8月 19 15:02 libevent.a lrwxrwxrwx 1 root root 26 8月 19 15:02 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9 -rwxr-xr-x 1 root root 585233 8月 19 15:02 libevent_core-2.0.so.5.1.9 -rw-r--r-- 1 root root 978626 8月 19 15:02 libevent_core.a lrwxrwxrwx 1 root root 26 8月 19 15:02 libevent_core.so -> libevent_core-2.0.so.5.1.9 lrwxrwxrwx 1 root root 27 8月 19 15:02 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9 -rwxr-xr-x 1 root root 404860 8月 19 15:02 libevent_extra-2.0.so.5.1.9 -rw-r--r-- 1 root root 593424 8月 19 15:02 libevent_extra.a lrwxrwxrwx 1 root root 27 8月 19 15:02 libevent_extra.so -> libevent_extra-2.0.so.5.1.9 lrwxrwxrwx 1 root root 29 8月 19 15:02 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.9 -rwxr-xr-x 1 root root 94281 8月 19 15:02 libevent_openssl-2.0.so.5.1.9 -rw-r--r-- 1 root root 131932 8月 19 15:02 libevent_openssl.a lrwxrwxrwx 1 root root 29 8月 19 15:02 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.9 lrwxrwxrwx 1 root root 30 8月 19 15:02 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9 -rwxr-xr-x 1 root root 18438 8月 19 15:02 libevent_pthreads-2.0.so.5.1.9 -rw-r--r-- 1 root root 18678 8月 19 15:02 libevent_pthreads.a lrwxrwxrwx 1 root root 30 8月 19 15:02 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9 lrwxrwxrwx 1 root root 21 8月 19 15:02 libevent.so -> libevent-2.0.so.5.1.9 -rw-r--r-- 1 root root 9458 8月 20 09:24 thread.h -rwxr-xr-x 1 root root 8008 8月 20 15:12 thread_test_1 -rwxr-xr-x 1 root root 8008 8月 20 15:12 thread_test_2 -rwxr-xr-x 1 root root 7976 8月 20 15:12 thread_test_3 -rw-r--r-- 1 root root 757 8月 20 10:22 thread_test.c [root@Betty thread_test]# [root@Betty thread_test]# ldd thread_test_* thread_test_1:linux-vdso.so.1 => (0x00007fff4a7ff000)libevent_core-2.0.so.5 => not foundlibevent_pthreads-2.0.so.5 => not foundlibpthread.so.0 => /lib64/libpthread.so.0 (0x000000388cc00000)libc.so.6 => /lib64/libc.so.6 (0x000000388c800000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) thread_test_2:linux-vdso.so.1 => (0x00007fffbc3ff000)libevent_core-2.0.so.5 => not foundlibevent_pthreads-2.0.so.5 => not foundlibpthread.so.0 => /lib64/libpthread.so.0 (0x000000388cc00000)libc.so.6 => /lib64/libc.so.6 (0x000000388c800000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) thread_test_3:linux-vdso.so.1 => (0x00007fffef754000)libevent_core-2.0.so.5 => not foundlibevent_pthreads-2.0.so.5 => not foundlibc.so.6 => /lib64/libc.so.6 (0x000000388c800000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) [root@Betty thread_test]# 可以看到,從結果上講,使用 -lpthread 和 -pthread 效果相同,而 -ptheads 不被支持。
最后,可以通過如下命令確認 libpthread 庫屬于哪個 rpm 包
[root@Betty thread_test]# rpm -qf /lib64/libpthread.so.0 glibc-2.12-1.149.el6_6.9.x86_64
PS:上面的實驗只是用于對于幾種選項配置方式,實際上?libevent_pthreads.so 內部已經包含了?libpthread.so 。
轉載于:https://my.oschina.net/moooofly/blog/495063
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【原创】多线程应用中pthread库使用问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos加单个ip和批量添加
- 下一篇: 南阳5--Binary String M