`
webcenterol
  • 浏览: 914948 次
文章分类
社区版块
存档分类
最新评论

Linux多线程Pthread学习小结

 
阅读更多

简介

POSIX thread 简称为pthread,Posix线程是一个POSIX标准线程.该标准定义内部API创建和操纵线程.

作用

线程库实行了POSIX线程标准通常称为pthreads.pthreads是最常用的POSIX系统如Linux和Unix,而微软Windowsimplementations同时存在.举例来说,pthreads-w32可支持MIDP的pthread   

Pthreads定义了一套 C程序语言类型、函数与常量,它以 pthread.h 头文件和一个线程库实现。

数据类型

pthread_t:线程句柄   

pthread_attr_t:线程属性

线程操纵函数(简介起见,省略参数)

pthread_create():创建一个线程   

pthread_exit():终止当前线程   

pthread_cancel():中断另外一个线程的运行   

pthread_join():阻塞当前的线程,直到另外一个线程运行结束   

pthread_attr_init():初始化线程的属性   

pthread_attr_setdetachstate():设置脱离状态的属性(决定这个线程在终止时是否可以被结合)

pthread_attr_getdetachstate():获取脱离状态的属性   

pthread_attr_destroy():删除线程的属性   

pthread_kill():向线程发送一个信号

同步函数

用于 mutex 和条件变量   

pthread_mutex_init() 初始化互斥锁   

pthread_mutex_destroy() 删除互斥锁   

pthread_mutex_lock():占有互斥锁(阻塞操作)   

pthread_mutex_trylock():试图占有互斥锁(不阻塞操作)。当互斥锁空闲时将占有该锁;否则立即返回  

pthread_mutex_unlock(): 释放互斥锁   

pthread_cond_init():初始化条件变量   

pthread_cond_destroy():销毁条件变量   

pthread_cond_wait(): 等待条件变量的特殊条件发生

pthread_cond_signal(): 唤醒第一个调用pthread_cond_wait()而进入睡眠的线程     

Thread-local storage(或者以Pthreads术语,称作 线程特有数据):   

pthread_key_create(): 分配用于标识进程中线程特定数据的键   

pthread_setspecific(): 为指定线程特定数据键设置线程特定绑定   

pthread_getspecific(): 获取调用线程的键绑定,并将该绑定存储在 value 指向的位置中   

pthread_key_delete(): 销毁现有线程特定数据键

与一起工作的工具函数

pthread_equal(): 对两个线程的线程标识号进行比较   

pthread_detach(): 分离线程   

pthread_self(): 查询线程自身线程标识号

详细请参见:

Linux多线程pthread: http://blog.csdn.net/Sunboy_2050/archive/2010/10/04/5920936.aspx

Pthread多线程学习小结: http://blog.csdn.net/Sunboy_2050/archive/2010/10/04/5921003.aspx

===================================================================

多线程创建

参考代码:

运行结果:

[work@db-testing-com06-vm3.db01.baidu.com pthread]$ gcc -Wall -o pthread_create pthread_create.c -lpthread

[work@db-testing-com06-vm3.db01.baidu.com pthread]$ ./pthread_create

main thread: pid: 12531 tid: 2505487232 (0x9556b380)

main thread: pid: 12531 tid: 2505487232 (0x9556b380)

new thread: pid: 12531 tid: 1084229984 (0x40a00960)

===================================================================

多线程条件变量

参考代码:

运行结果:

[work@db-testing-com06-vm3.db01.baidu.com pthread]$ gcc -Wall -o pthread_cond2 pthread_cond2.c -lpthread

[work@db-testing-com06-vm3.db01.baidu.com pthread]$ ./pthread_cond2

counter: 0

counter(main): 0

counter(decrement): 0

counter(increment): 0

counter++(before): 0

counter++(after): 1

counter--(before): 1

counter--(after): 0

counter(main): 1

counter(main): 2

counter(main): 3

counter(main): 4

counter(main): 5

counter(main): 6

counter(main): 7

counter(main): 8

counter(main): 9

详细解释,请见:http://blog.csdn.net/Sunboy_2050/archive/2010/11/24/6031723.aspx

===================================================================

多线程的创建特殊数据键

参考代码:

运行结果:

[work@db-testing-com06-vm3.db01.baidu.com pthread]$ gcc -Wall -o pthread_setspecific pthread_setspecific.c -lpthread
[work@db-testing-com06-vm3.db01.baidu.com pthread]$ ./pthread_setspecific
hello main
child_1: thread 1084229984 enter
child_1: thread 1084229984 returns 0x40a00960
child_2: thread 1094719840 enter
child_2: thread 1094719840 returns 0x41401960
destruct executed in thread = 1084229984, arg = 0x40a00960
destruct executed in thread = 1094719840, arg = 0x41401960
bye main

附加参考——函数原型:

Posix定义了两个API分别用来创建和注销TSD:

int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *))
注销一个TSD采用如下API:
int pthread_key_delete(pthread_key_t key)
int pthread_setspecific(pthread_key_t key, const void *pointer)
void * pthread_getspecific(pthread_key_t key)
参考网址:

===================================================================

多线程的创建特殊数据键

参考代码:

运行结果:

work@db-testing-com06-vm3.db01.baidu.com pthread]$ gcc -Wall -o pthread_once pthread_once.c -lpthread
[work@db-testing-com06-vm3.db01.baidu.com pthread]$ ./pthread_once
hello main
Func: once_run in thread: 1084229984
child_1: thread 1084229984 returns
child_2: thread 1094719840 returns
bye main

分享到:
评论

相关推荐

    linux多线程开发区别与window

    本文中我们从 5 个方面总结出 Linux 多线程编程上的问题,并分别引出相关改善的开发经验,用以避免这些的陷阱。我们希望这些经验可以帮助读者们能更好更快的熟悉 Linux 平台的多线程编程。 我们假设读者都已经很...

    linux系统编程之线程.zip

    【练习】:编写多线程程序,总结exit、return、pthread_exit各自退出效果。 return:返回到调用者那里去。 pthread_exit():将调用该函数的线程 exit: 将进程退出。 pthread_join函数 阻塞等待线程退出,获取...

    Linux多线程环境下 关于进程线程终止函数总结

    pthread_kill与kill有区别,是向线程发送signal。,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理函数。 int pthread_kill(pthread_t thread, int sig); 向指定ID的线程...

    Linux系统编程-(pthread)线程通信(条件变量).pdf

    **读写锁总结:** 1. 读写锁分为读锁和写锁。 2. 如果资源被读写锁保护,多个线程可以同时获取读锁—也就是读支持多个线程同时读。 3. 资源加了写锁之后,在写资源的时候只能被一个线程占用,其他读锁就会阻塞。 ...

    Linux线程退出方式总结(推荐)

    在编写多线程代码时,经常面临线程安全退出的问题。 一般情况下,选择检查标志位的方式: 在线程的while循环中,执行完例程后,都对标志位进行检查,如果标志位指示继续执行则再次执行例程,如果标志位设置为...

    linux网络编程-宋敬彬-part3

    4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...

    linux网络编程-宋敬彬-part2

    4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...

    Win丨linux丨操作系统实验二:生产者——消费者问题

    2. 在Linux操作系统上,利用Pthread API提供的信号量机制,编写应用程序实现生产者——消费者问题。 3. 两种环境下,生产者和消费者均作为独立线程,并通过empty、full、mutex三个信号量实现对缓冲进行插入与删除。 ...

    linux网络编程-宋敬彬-part1

    4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...

    深入分析父子线程、进程终止顺序不同产生的结果

    一、线程Linux线程创建函数为pthread_create(),默认规则是谁创建子线程,谁就要负责子线程的资源回收,当父线程退出后,子线程也随着退出。所以,一般情况下,父线程退出时都要确保子线程已经退出,所以会使用...

    linux网路编程 中文 23M 版

    第1 章Linux操作系统概述................... .......................................................................... 2 1.1 Linux发展历史........................................................ 2 ...

Global site tag (gtag.js) - Google Analytics