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的更多相关文章

  1. 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...

  2. Windows下使用Dev-C++开发基于pthread.h的多线程程序

    一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录      我选择的目录是:E:\DEV-C ...

  3. NPTL vs PThread

    NPTL vs PThread POSIX threads (pthread) is not an implementation, it is a API specification (a stand ...

  4. Linux pthread

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h& ...

  5. VS2013 配置pthread

    参考:http://blog.csdn.net/qianchenglenger/article/details/16907821 一.下载地址 ftp://sourceware.org/pub/pth ...

  6. Linux Pthread 深入解析(转-度娘818)

    Linux Pthread 深入解析   Outline - 1.线程特点 - 2.pthread创建 - 3.pthread终止         - 4.mutex互斥量使用框架         - ...

  7. pthread 学习

    1. 创建线程 int pthread_create (pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void*), ...

  8. pthread——pthread_cleanup

    Pthread_cleanup用于注册线程清理函数,注册的清理函数将在线程被取消或者主动调用pthread_exit时被调用:     一个简单的示例: #include <pthread.h& ...

  9. 多线程(pthread、NSThread、GCD)

    pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...

  10. linux的<pthread.h>

    转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...

随机推荐

  1. 结构体dict_index_t;

    /** InnoDB B-tree index */ typedef struct dict_index_struct dict_index_t; /** Data structure for an ...

  2. [POJ 1155] TELE

    TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3787   Accepted: 2007 Description ...

  3. App Store 加急审核方式

    https://developer.apple.com/contact/app-store/?topic=expedite 1:理由一般是用户安全问题或者崩溃问题成功率会高一些. 如果是崩溃问题,你最 ...

  4. MongoDB之三(高级操作 聚合、游标)

    一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...

  5. Android从零单排之免费短信验证

    介绍 短信验证功能大家都很熟悉了.在很多地方都能见到,注册新用户或者短息验证支付等.短信验证利用短信验证码来注册会员,大大降低了非法注册,很大程度上提高了用户账户的安全性. 目前市面上已经有了很多提供 ...

  6. The Donkey of Gui Zhou

    Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...

  7. HW2.4

    import java.util.Scanner; public class Solution { public static void main(String[] args) { final dou ...

  8. POJ-1151 Atlantis 矩形面积并

    题目链接:http://poj.org/problem?id=1151 扫描线+离散+线段树,线段树每个节点保存的是离散后节点右边的线段. //STATUS:C++_AC_16MS_208KB #in ...

  9. Windows 2003 VPN配置步骤[转]

    一,服务器端:Windows 2003 VPN代理的配置方法如下: 1.关闭防火墙,用“管理工具”中的“服务”将“Windows Firewall/Internet Connection Sharin ...

  10. leptonica使用问题

    想要使用leptonica编写一个图像处理程序,或者调用leptonica/prog下例子程序,出现类似错误: leptTest ./test Error in pixReadStreamJpeg: ...