Linux多线程Pthread学习小结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux多线程Pthread学习小结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9732字,纯文字阅读大概需要14分钟。
内容图文
![Linux多线程Pthread学习小结](/upload/InfoBanner/zyjiaocheng/976/02cdf317b58e4e9e8a7c857100811dd7.jpg)
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
简介
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) 参考网址: http://blog.sina.com.cn/s/blog_46d528490100lxm0.html http://xunet.blog.51cto.com/138167/22011(推荐)
===================================================================
多线程的创建特殊数据键
参考代码:
运行结果:
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
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
内容总结
以上是互联网集市为您收集整理的Linux多线程Pthread学习小结全部内容,希望文章能够帮你解决Linux多线程Pthread学习小结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。