【原创】多线程应用中pthread库使用问题
2023-09-14 08:59:46 时间
在 linux 下开发多线程应用,大多情况下我们都会使用 pthread (即 POSIX thread)这个库。该库遵循 POSIX.1-2001 标准。
在使用该库的过程中,肯定有人见过各种 gcc 选项配置方式:
-pthread -pthreads -lpthread 但应该有很多朋友没有搞清楚,这几种方式的区别和联系。本文的目的就在于此。
在 GCC man 手册中,我们可以找到如下内容
?
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
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
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 选项,可以参考下图中的说明
![](http://static.oschina.net/uploads/space/2015/0820/150849_7FmC_617889.png)
下面实验验证一下,几种方式的效果
?
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
配置为:../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)
-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]# 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
-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]#
可以看到,从结果上讲,使用 -lpthread 和 -pthread 效果相同,而 -ptheads 不被支持。
最后,可以通过如下命令确认 libpthread 库属于哪个 rpm 包
?
PS:上面的实验只是用于对于几种选项配置方式,实际上 libevent_pthreads.so 内部已经包含了 libpthread.so 。
Go Mutex:保护并发访问共享资源的利器 本文主要介绍了 Go 语言中互斥锁 Mutex 的概念、对应的字段和方法、基本使用和易错场景,最后基于 Mutex 实现一个简单的线程安全的缓存。
Python核心基础必备(多线程、多进程编程)(Queue,Lock/Rlock,Condition,Semaphore) 一个人活在这个世界上为了什么呢?我觉得是去经历和享受。对于没做过的事情要做一做。 每个人在年轻的时候,所做出的的选择是没有对错之分的,所有的选择都是对的,只能说对于所做选择的结果,只是好与更好的差别。每个人都有自己衡量事物的价值观,我们有什么样的认知就会投影出什么样的图像,所以一定要不断超越有限的认知,不断地提升内外的自由度,不要尝试让自己假装看起来很努力,因为结果不会陪你演戏! 学习如逆水行舟 不进则退
Java并发编程笔记之Semaphore信号量源码分析 JUC 中 Semaphore 的使用与原理分析,Semaphore 也是 Java 中的一个同步器,与 CountDownLatch 和 CycleBarrier 不同在于它内部的计数器是递增的,那么,Semaphore 的内部实现是怎样的呢? Semaphore 信号量也是Java 中一个同步容器,与CountDownLatch 和 CyclicBarrier 不同之处在于它内部的计数器是递增的。
相关文章
- 多线程应用
- Java中的多线程和多进程
- 2-多线程
- Java多线程基本使用及模拟购票
- C++ 新特性学习(八) — 原子操作和多线程库[多工内存模型]
- Python Tkinter Gui 运行不卡顿,多线程解决界面卡死问题
- Python多线程爬虫编程中queue.Queue和queue.SimpleQueue的区别和应用
- linux下多线程开发
- Linux多线程编程详细解析—-条件变量 pthread_cond_t详解程序员
- 多线程详解编程语言
- 多线程MySQL:强力运行你的应用(多线程mysql)
- Linux 下多线程编程技术实现(linux调用线程)
- 应用嵌入式Linux:开发有效的多线程应用(嵌入式linux多线程)
- 的Redis:安全的多线程应用(redis是线程安全)
- 机制Linux多线程编程:锁机制的应用(linux多线程的锁)
- Linux UDP多线程编程的应用(linuxudp线程)
- 使用Linux操作系统创建高效的多线程程序(linux多线程程序)
- Linux C:多线程编程的精髓(linux c 线程)
- 优化网站性能-采用多线程Redis解决方案(多线程 redis)
- 极大提高效率:SQLServer多线程应用(sqlserver多线程)
- 理解Redis多线程从实际应用出发(怎么理解redis多线程)
- Redis锁技术优化多线程应用(redis锁多线程)
- Redis过期优化多线程应用(redis过期 多线程)
- Redis过期解决方案多线程技术实现(redis过期 多线程)
- Python多线程同步Lock、RLock、Semaphore、Event实例