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下的多 ...
随机推荐
- 函数page_cur_search_with_match
/****************************************************************//** Searches the right position fo ...
- 加快VisualStudio的开发速度--VS的一些开发技巧
最近不得不使用VisualStudio来进行一些开发,用习惯了Eclipse,感觉VS很难上手,不过通过google,把VS进行Eclipse化,顺便记录下来,以防忘记. 1)显示文字的自动完成. 改 ...
- JAVA web选型
Web服务器是运行及发布Web应用的容器,只有将开发的Web项目放置到该容器中,才能使网络中的所有用户通过浏览器进行访问.开发Java Web应用所采用的服务器主要是与JSP/Servlet兼容的We ...
- apache开源项目-- NiFi
Apache NiFi 是一个易于使用.功能强大而且可靠的数据处理和分发系统.Apache NiFi 是为数据流设计.它支持高度可配置的指示图的数据路由.转换和系统中介逻辑. 架构: 集群管理器: 主 ...
- CodeForcesGym 100753B Bounty Hunter II 二分图最小路径覆盖
关键在建图 题解:http://www.cnblogs.com/crackpotisback/p/4856159.html 学习:http://www.cnblogs.com/jackiesteed/ ...
- HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜
此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Me ...
- JSP-讲解(生成java类、静态导入与动态导入)
一.JSP技术简介 JSP是Java Server Page的缩写,它是Servlet的扩展,它的作用是简化网站的创建和维护. JSP是HTML代码与Java代码的混合体. JSP文件通常以JSP或J ...
- HDU 1117 免费馅饼 二维动态规划
思路:a[i][j]表示j秒在i位置的数目,dp[i][j]表示j秒在i位置最大可以收到的数目. 转移方程:d[i][j]=max(dp[i-1][j],dp[i-1][j-1],dp[i-1][j+ ...
- 使用 Windows PowerShell 管理Windows Azure映像
你可以使用 Azure PowerShell 模块中的 cmdlet 管理可供你的 Azure 订阅使用的映像.这包括 Azure 提供的映像以及你上载的映像.对于某些映像任务,你还可以使用 Azur ...
- 合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数 (转)
转自: http://www.cnblogs.com/aolin/archive/2011/04/12/2014122.html 示例表 tb 数据如下 id value—————1 aa1 bb2 ...