pthread
pthread是UNIX操作系统中创建和控制线程的一系列API,通过了解这些API,可以更加清晰的理解线程究竟是什么。
调用pthread的API首先要包含<pthread.h>这一头文件,以下为pthread内的基础API。
1、pthread_create(创建线程)
int pthread_create(pthread_t *restrict tidp,
<span style="white-space:pre"> </span> const pthread_attr_t *restrict attr,
void *(*start_rtn)(void*),void *restrict arg);
tidp::线程的ID,主线程从中获得新建线程的ID;
attr:控制线程的属性,置为NULL,表示使用默认属性;
start_rtn:新建线程运行的函数的入口地址;
arg:函数所需的参数。
由此可见,在实际操作中,线程的执行部分就是一个函数。
2、pthread_exit(终止线程)
线程有三种终止方式:1、正常返回;2、被其它线程取消;3、线程本身调用pthread_exit。
void pthread_exit(void *rval_ptr);
rval_ptr:线程终止时返回的信息。
3、pthread_join(等待其它线程的结束)
int pthread_join(pthread_t thread, void **rval_ptr);
thread:要等待的线程ID;
rval_ptr:获得线程终止时返回的信息,NULL则表示不接受返回的信息。
线程调用pthread_join时,自身被阻塞,当等待的线程终止时,该线程才被唤醒。
4、pthread_cancel(取消其它进程)
int pthread_cancel(pthread_tid);
tid:要取消的线程ID;
pthread的更多相关文章
- 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...
- Windows下使用Dev-C++开发基于pthread.h的多线程程序
一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录 我选择的目录是:E:\DEV-C ...
- NPTL vs PThread
NPTL vs PThread POSIX threads (pthread) is not an implementation, it is a API specification (a stand ...
- Linux pthread
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h& ...
- VS2013 配置pthread
参考:http://blog.csdn.net/qianchenglenger/article/details/16907821 一.下载地址 ftp://sourceware.org/pub/pth ...
- Linux Pthread 深入解析(转-度娘818)
Linux Pthread 深入解析 Outline - 1.线程特点 - 2.pthread创建 - 3.pthread终止 - 4.mutex互斥量使用框架 - ...
- pthread 学习
1. 创建线程 int pthread_create (pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void*), ...
- pthread——pthread_cleanup
Pthread_cleanup用于注册线程清理函数,注册的清理函数将在线程被取消或者主动调用pthread_exit时被调用: 一个简单的示例: #include <pthread.h& ...
- 多线程(pthread、NSThread、GCD)
pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...
- linux的<pthread.h>
转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...
随机推荐
- Webform——页面局部刷新
有一些数据控件,每次更改它的值后,都会重新查询数据库,然后再重新显示出来. 这样每次都会刷新全部页面,如果是一些信息量很庞大的页面,就会出现卡顿的现象,为了避免这种情况,就用到了局部刷新. 所用到的控 ...
- WPF——绑定数据库数据(Listview)
一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...
- BZOJ_3039_玉蟾宫_(动态规划+悬线法)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3039 n*m的矩阵由R和F组成,求全是F的子矩阵的大小的三倍. 分析 悬线法: 浅谈用极大化思 ...
- -_-#【Cookie】使用无 cookies 的域
cookie-free域名提高网页效率
- mysql shell 备份脚本
使用 mysqldump 备份数据库,通过 FTP 上传到备份服务器,同时在本地保留备份文件. 新建一个 Shell 脚本文件 vi /home/work/backup.sh 添加如下内容: #! / ...
- 【转】为Xcode添加删除行、复制行快捷键
原文网址:http://www.jianshu.com/p/cc6e13365b7e 在使用eclipse过程中,特喜欢删除一行和复制一行的的快捷键.而恰巧Xcode不支持这两个快捷键,再一次的恰巧让 ...
- imdisk命令行使用及配置
imdisk是一个开源的虚拟磁盘软件,集虚拟光驱,文件虚拟光驱,映射物理磁盘,映射物理内存等功能 如果使用devio--Device I/O Service,可以映射网络磁盘等. 通用于windows ...
- 【CSS】Intermediate6:Display
1display :inline/block/none 2.inline value Cause all list items in a list to appear next to each oth ...
- sap判断条件
EQ 等于= 等于NE 不 等于<> 不 等于>< 不 等于LT 小 于< 小于LE 小 于等于<= 小 于等于GT 大 于> 大于GE ...
- 综合而强大的DATASNAP
从DELPHI2009开始,DATASNAP技术上完全是全新的架构,多层架构不再基于微软的COM,摆脱COM就等于摆脱了WINDOWS的束缚. TCP/IP通信不再需要先开启scktsrvr.exe程 ...