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

【原创】多线程应用中pthread库使用问题相关推荐

  1. pthread库进行多线程编程 - 组件工厂 - C++博客

    pthread库进行多线程编程 - 组件工厂 - C++博客 11 Threads 1 Introduction 不用介绍了吧- 2 Thread Concepts 1.     Thread由下面部 ...

  2. C语言中pthread或Windows API在多线程编程中的基本应用

    文章目录 多线程概述 掌握多线程需要学习什么? 使用pthread.h实现多线程 使用Windows API实现多线程 使用threads.h实现多线程 参考资料 警告 由于我懒得写完,而且懂的也不是 ...

  3. c语言 标准多线程库,关于C语言多线程pthread库的功能描述

    与线程相关的说明 һpthread_t pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义: typedef unsigned long int pthr ...

  4. 线程以及pthread库的使用

    https://blog.csdn.net/weixin_38102771/article/details/91351126 https://blog.csdn.net/qq_29677867/art ...

  5. Linux 多线程应用中编写安全的信号处理函数

    2019独角兽企业重金招聘Python工程师标准>>> Linux 多线程应用中编写安全的信号处理函数 在 开发多线程应用时,开发人员一般都会考虑线程安全,会使用 pthread_m ...

  6. 多线程程序中操作的原子性

    0. 背景 原子操作就是不可再分的操作.在多线程程序中原子操作是一个非常重要的概念,它常常用来实现一些同步机制,同时也是一些常见的多线程Bug的源头. 本文主要讨论了三个问题: 1. 多线程程序中对变 ...

  7. windows下使用pthread库

    最近在看<C++多核高级编程>这本书,收集了些有用的东西,方便在windows下使用POSIX标准进行Pthread开发,有利于跨平台. ------------------------- ...

  8. c++ sleep函数_Linux 多线程应用中如何编写安全的信号处理函数

    关于代码的可重入性,设计开发人员一般只考虑到线程安全,异步信号处理函数的安全却往往被忽略.本文首先介绍如何编写安全的异步信号处理函数:然后举例说明在多线程应用中如何构建模型让异步信号在指定的线程中以同 ...

  9. Linux 多线程应用中如何编写安全的信号处理函数

    Linux 多线程应用中编写安全的信号处理函数 在开发多线程应用时,开发人员一般都会考虑线程安全,会使用 pthread_mutex 去保护全局变量.如果应用中使用了信号,而且信号的产生不是因为程序运 ...

最新文章

  1. java debugtrace_Debug与Trace的区别
  2. 北大智能学院成立!AI视觉泰斗朱松纯教授任院长
  3. hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)
  4. QML基础类型之var
  5. Linux命令中的参数,linux中一些命令以及一些参数的用法
  6. mysql怎么改字体编码_mysql怎么改字符编码?
  7. 16.看板方法——三类改进机会笔记
  8. Deepin2014 QT Creator安装
  9. git如何合并指定文件内容_Git合并指定文件到另一个分支
  10. 微软在线实验室启用谷歌的reCAPTCHA,我们又丢失了一个好东东
  11. 严重BS骗样本的骗子
  12. HoverTree.Model.ArticleSelect类的作用
  13. 最简单的视频编码器:基于libx265(编码YUV为H.265)
  14. 结构体初始化的四种方法
  15. 随机矩阵stochastic matrix和双随机矩阵 doubly stochastic matrix 和bistochastic matrix
  16. ALVA Systems发布AR新品 倪光南院士致辞
  17. 怎样从任务管理器打开计算机,怎么打开任务管理器|任务管理器在哪里打开
  18. ethercat从站io输入输出模块,lan9252,stm32f407,提供原理图,源代码及相关资料
  19. 友豆火山CPG插件开发003-顶级对象
  20. WIN7 错误代码0x80070005怎么办

热门文章

  1. u盘读写测试_如何提高u盘读写速度 提高u盘读写速度方法【详细步骤】
  2. python 之GUI设计:Entry组件
  3. 微信小程序实时将less编译为wxss
  4. 关于IE7 兼容问题
  5. 团队作业5-Alpha版本测试报告(彼岸芳华队)
  6. mybatis工具类
  7. javascript string replace 正则替换
  8. SSM整合框架实现ajax校验
  9. 文档生成工具Sandcastle Help File Builder
  10. 营销推广中心设计(一)营销架构与策略