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. 【转】Android学习系列(1)--为App签名(为apk签名)

    原文网址:http://www.cnblogs.com/qianxudetianxia/archive/2011/04/09/2010468.html 写博客是一种快乐,前提是你有所写,与人分享,是另 ...

  2. ArcSDE 10.1安装、配置、连接 (SQL Server 2008)

    转自:http://blog.csdn.net/esrichinacd/article/details/8510224 1  概述 ArcSDE 10.1的安装配置相较于ArcSDE 10.0和之前版 ...

  3. clone Control event handlers at run time

    var btn2 =newButton(); btn2.Text= btn1.Text; btn2.size = btn1.size; To clone all events of any WinFo ...

  4. How to get FlowLayoutPanel.AutoSize to work with FlowBreak

    have a problem with a FlowLayoutPanel and I don't know how to solve it. I'm placing two FlowLayoutPa ...

  5. 基于Geoserver配置多图层地图以及利用uDig来进行样式配置

    在GeoServer中配置多个图层的地图相对来说很容易,其步骤为: 1. 进入geoserver 2. 配置相关的FeatureTypes 3. 配置WMS内容,进入以后,主要有以下几个地方需要命名: ...

  6. Clean Code – Chapter 5 Formatting

    The Purpose of Formatting Code formatting is about communication, and communication is the professio ...

  7. NOIP 2013 花匠

    有多种方案,找拐点数目最简单O(n) 注意此题有相邻点价值一样,代码改变一点 #include <cstdio> #include<iostream> #include< ...

  8. 【转】终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件

    感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...

  9. 如何用jQuery实现在鼠标滚动后导航栏保持固定

    要实现如下效果,鼠标滚动后,上方导航栏置顶固定 关键html代码: <div class="header-bottom"> <div class="co ...

  10. Robot Framework自动化测试(三)--- 封装系统关键字

    之前对robotframework-ride了解的不多,后来知道了引入Selenium2Lirary库后可以做web UI自动化测试,但发现和python没啥关系,今天学习了封装系统关键字算是和pyt ...