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下的多 ...
随机推荐
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- Google Chrome中的高性能网络(一)
以下内容是"The Performance of Open Source Applications" (POSA)的草稿, 也是The Architecture of Open S ...
- .Net 垃圾回收机制原理(一)
英文原文:Jeffrey Richter 编译:赵玉开 链接:http://www.cnblogs.com/yukaizhao/archive/2011/11/23/dot_net_GC_1.html ...
- 使用 EPUB 制作数字图书
基于 XML 的开放式 eBook 格式 是否需要分发文档.创建电子图书或者把喜欢的博客文章存档?EPUB 是一种开放式的数字图书规范,以常用的技术如 XML.CSS 和 XHTML 为基础,EPUB ...
- PowerDesigner一些小技巧
1.安装PD v12.0版 2.由pdm生成建表脚本时,字段超过15字符就发生错误(oracle) 原因未知,解决办法是打开PDM后,会出现Database的菜单栏,进入Database - Edit ...
- java项目中eclipse控制台输出log4j的信息
最近做的一个hadoop项目中,用MR实现了一个比较复杂的问题,其中的日志信息都是使用的是log4j来处理的.但不知怎么控制台不输出日志信息,只能输出System.out.println()信息,这个 ...
- CentOS设置在同一窗口打开文件夹
默认情况下,CentOS双击文件夹会打开一个新窗口.这对于习惯Windows的用户会感觉非常别扭.其实,如果用鼠标中键双击文件夹,就不会打开新窗口了.当然,也可以按照如下设置: 1. 打开任意一个文 ...
- cnUVA情况
http://cn_uva.jd-app.com/ 欢迎访问
- How to select a OptionSet on ms sqlserver database for Microsoft Dynamics CRM
OptionSet: Select * from StringMap Global OptionSet: select os.Name, l.Label from AttributePicklist ...
- 在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕分辨率非常小,操作非常不便。通过安装VirtualBox提供的“增强功能组件”,-摘自网络
在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕分辨率非常小,操作非常不便.通过安装VirtualBox提供的“增强功能组件”,可以解决这一问题,并且使用非常方便. 一.环境 | En ...