【原创】多线程应用中pthread库使用问题

简介:

在 linux 下开发多线程应用,大多情况下我们都会使用 pthread (即 POSIX thread)这个库。该库遵循 POSIX.1-2001 标准。  

在使用该库的过程中,肯定有人见过各种 gcc 选项配置方式:  
  • -pthread
  • -pthreads
  • -lpthread
但应该有很多朋友没有搞清楚,这几种方式的区别和联系。本文的目的就在于此。  

在 GCC man 手册中,我们可以找到如下内容  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
Machine Dependent Options
...
HPPA Options
...
        -threads
            Add support for multithreading with the dce thread library under HP-UX.  This option sets flags for both the preprocessor and linker.
...
IA-64 Options
...
        -pthread
            Add 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
...
        -pthread
            Adds support for multithreading with the pthreads library.  This option sets flags for both the preprocessor and linker.
...
SPARC Options
...
        -threads
            Add 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.
 
        -pthreads
            Add 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.
 
        -pthread
            This is a synonym for -pthreads.
...
可以看到,这几个选项都是  Machine Dependent   的。而什么是 Machine Dependent ,则可以参考   这里    

还有以下内容  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
Options for Linking
...
        -llibrary
        -l library
            Search 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 specified
            precisely 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 members
            which 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 only
            difference 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 选项,可以参考下图中的说明  

 

下面实验验证一下,几种方式的效果  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
[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 found
         libevent_pthreads-2.0.so.5 => not found
         libpthread.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 found
         libevent_pthreads-2.0.so.5 => not found
         libpthread.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 found
         libevent_pthreads-2.0.so.5 => not found
         libc.so.6 => /lib64/libc .so.6 (0x000000388c800000)
         /lib64/ld-linux-x86-64 .so.2 (0x000000388c400000)
[root@Betty thread_test] #
可以看到,从结果上讲,使用 -lpthread 和 -pthread 效果相同,而 -ptheads 不被支持。  

最后,可以通过如下命令确认 libpthread 库属于哪个 rpm 包  
?
1
2
[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 。


目录
相关文章
|
1月前
|
并行计算 安全 程序员
【C++】—— C++11之线程库
【C++】—— C++11之线程库
|
1月前
|
Java 调度 Android开发
构建高效Android应用:探究Kotlin多线程编程
【2月更文挑战第17天】 在现代移动开发领域,性能优化一直是开发者关注的焦点。特别是在Android平台上,合理利用多线程技术可以显著提升应用程序的响应性和用户体验。本文将深入探讨使用Kotlin进行Android多线程编程的策略与实践,旨在为开发者提供系统化的解决方案和性能提升技巧。我们将从基础概念入手,逐步介绍高级特性,并通过实际案例分析如何有效利用Kotlin协程、线程池以及异步任务处理机制来构建一个更加高效的Android应用。
35 4
|
1月前
|
API 数据库 Android开发
构建高效Android应用:探究Kotlin多线程优化策略
【2月更文挑战第14天】随着移动设备性能的日益强大,用户对应用程序的响应速度和流畅性要求越来越高。在Android开发中,合理利用多线程技术是提升应用性能的关键手段之一。Kotlin作为一种现代的编程语言,其协程特性为开发者提供了更为简洁高效的多线程处理方式。本文将深入探讨使用Kotlin进行Android多线程编程的最佳实践,包括协程的基本概念、优势以及在实际项目中的应用场景和性能优化技巧,旨在帮助开发者构建更加高效稳定的Android应用。
|
1月前
|
缓存 安全 Java
保障线程安全性:构建可靠的多线程应用
保障线程安全性:构建可靠的多线程应用
|
3月前
|
监控 Linux 编译器
多线程死锁检测的分析与实现(linux c)-有向图的应用
在日常的软件开发中,多线程是不可避免的,使用多线程中的一大问题就是线程对锁的不合理使用造成的死锁,死锁一旦发生,将导致多线程程序响应时间长,吞吐量下降甚至宕机崩溃,那么如何检测出一个多线程程序中是否存在死锁呢?在提出解决方案之前,先对死锁产生的原因以及产生的现象做一个分析。最后在用有向环来检测多线程中是否存在死锁的问题。
56 0
|
15天前
|
Java
深入理解Java并发编程:线程池的应用与优化
【4月更文挑战第3天】 在Java并发编程中,线程池是一种重要的资源管理工具,它能有效地控制和管理线程的数量,提高系统性能。本文将深入探讨Java线程池的工作原理、应用场景以及优化策略,帮助读者更好地理解和应用线程池。
|
1月前
|
Java 调度 Python
深入解析 Python asyncio 库:如何使用线程池实现高效异步编程
深入解析 Python asyncio 库:如何使用线程池实现高效异步编程
43 0
|
4天前
|
Java API 调度
安卓多线程和并发处理:提高应用效率
【4月更文挑战第13天】本文探讨了安卓应用中多线程和并发处理的优化方法,包括使用Thread、AsyncTask、Loader、IntentService、JobScheduler、WorkManager以及线程池。此外,还介绍了RxJava和Kotlin协程作为异步编程工具。理解并恰当运用这些技术能提升应用效率,避免UI卡顿,确保良好用户体验。随着安卓技术发展,更高级的异步处理工具将助力开发者构建高性能应用。
|
5天前
|
Java
探秘jstack:解决Java应用线程问题的利器
探秘jstack:解决Java应用线程问题的利器
14 1
探秘jstack:解决Java应用线程问题的利器
|
16天前
|
安全 Java 容器
Java并发编程:实现高效、线程安全的多线程应用
综上所述,Java并发编程需要注意线程安全、可见性、性能等方面的问题。合理使用线程池、同步机制、并发容器等工具,可以实现高效且线程安全的多线程应用。
14 1

热门文章

最新文章