一、线程标识
     和每个进程都有一个进程ID一样,每个线程也有一个线程ID,线程ID是以pthread_t数据类型来表示的,在Linux中,用无符号长整型表示pthread_t,Solaris 把phread_t数据类型表示为无符号整型,FreeBSD 和Mac OS X 用一个指向pthread结构的指针来表示pthread_t数据类型。
     可以使用pthread_self函数获得自身的线程ID。   
#include <pthread.h>
pthread_t pthread_self(void);
 
二、线程创建
     使用pthread_create函数创建新线程 
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_arrt_t *restrict addr, void *(*start_rtn)(void *), void *restrict arg);
     当pthread_create成功返回后,新创建线程的线程ID会被设置成tidp指向的内存单元,attr参数用于定制各种不同的线程属性,后面再讨论线程属性,现在先把它置为null,创建一个具有默认属性的线程。
     新创建的线程从start_rtn函数开始运行,该函数接收一个无类型指针的参数arg,如果要传给它的参数多于一个,可以把参数放到一个结构中,然后把结构的地址作为arg传入。
     线程新建后会继承调用线程的浮点环境和屏蔽字。
例子:  
#include "apue.h"
#include <pthread.h> pthread_t ntid; void printids(const char *s)
{
pid_t pid;
pthread_t tid; pid = getpid();
tid = pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
} void *thr_fn(void *arg)
{
printids("new thread: ");
return ((void *));
} int main(void)
{
int err; err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != )
{
err_exit(err, "can't create thread");
} printids("main thread: ");
sleep();
exit();
}
  这个程序有两个特别的地方:第一,主线程需要休眠,如果主线程不休眠,主线程会退出,新线程并没有机会运行。第二,新线程通过pthread_self(),获得自己的线程ID。
./a.out
main thread: pid tid (0xb75e36c0)
new thread: pid tid (0xb75e2b40)
  虽然Linux线程ID是用无符号长整型来表示的,但它们看起来更像指针。
 
三、线程终止
     如果任意线程调用了exit,_exit,_Exit,整个进程都会终止,这个要注意。
     单个线程可以通过以下三种方式退出,且不终止整个进程。
     1.线程可以简单地从启动例程中返回,返回值是线程的退出码。
     2.线程可以被同一进程中的其他线程取消。
     3.调用pthread_exit
 
     先来看pthread_exit退出的情况。
#include <pthread.h>
void pthread_exit(void *rval_ptr);

ravl_ptr是无类型指针,进程中的其他线程可以通过pthread_join函数获得这个指针。

#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);

调用线程将一直阻塞,直至指定的线程退出,rval_ptr就包含返回码,如果线程被取消,rval_ptr指定的内存单元就设置为PTHREAD_CANCELED.可以通过调用pthread_join自动把线程置于分离状态,如果线程已处于分离状态,pthread_join就会调用失败。

例子:
#include "apue.h"
#include <pthread.h> void *thr_fn1(void *arg)
{
printf("thread 1 returning\n");
return (void *);
} void *thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *));
} int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != )
{
err_exit(err, "can't create thread1");
} err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != )
{
err_exit(err, "can't create thread2");
} err = pthread_join(tid1, &tret); if (err != )
{
err_exit(err, "can't join thread1");
} printf("thread1 exit code:%ld\n", (long)tret); err = pthread_join(tid2, &tret); if (err != )
{
err_exit(err, "can't join thread2");
} printf("thread2 exit code:%ld\n", (long)tret); return ;
}
./a.out
thread exiting
thread returning
thread1 exit code:
thread2 exit code:
  也可传递包含复杂消息的结构的地址,不过必须注意,这个结构所使用的内存必须在完成调用后仍是有效的。
      线程也可以调用pthread_cancel函数来请求取消同一进程的其他线程
#include <pthread.h>
int pthread_cancel(pthread_t tid);

听着有点霸道,不过也只是请求而已,线程可以选择忽略这个请求。

     线程可以安排它退出时需要调用的函数,这样的函数是由pthread_cleanup_push注册在栈中的,所以执行顺序与注册时相反。
#include <pthread.h>
void pthread_cleanup_push(void(*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);

当线程执行以下动作时,清理函数rtn由pthread_cleanup_push函数调度

     1.调用pthread_exit时
     2.响应取消请求时
     3.用非零execute参数调用pthread_cleanup_pop时。
例子:
#include "apue.h"
#include <pthread.h> void cleanup(void *arg)
{
printf("cleanup: %s\n", (char *)arg);
} void *thr_fn1(void *arg)
{
printf("thread 1 start\n");
pthread_cleanup_push(cleanup, "thread 1 first handler");
pthread_cleanup_push(cleanup, "thread 1 second handler");
printf("thread 1 push complete\n"); if (arg)
{
return (void *);
} pthread_cleanup_pop();
pthread_cleanup_pop(); return (void *);
} void *thr_fn2(void *arg)
{
printf("thread 2 start\n");
pthread_cleanup_push(cleanup, "thread 2 first handler");
pthread_cleanup_push(cleanup, "thread 2 second handler");
printf("thread 2 push complete\n"); if (arg)
{
pthread_exit((void *));
} pthread_cleanup_pop();
pthread_cleanup_pop(); return (void *);
} int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; err = pthread_create(&tid1, NULL, thr_fn1, (void *));
if (err != )
{
err_exit(err, "can't create thread 1");
} err = pthread_create(&tid2, NULL, thr_fn2, (void *));
if (err != )
{
err_exit(err, "can't create thread 2");
} err = pthread_join(tid1, &tret);
if (err != )
{
err_exit(err, "can't join with thread 1");
}
printf("thread 1 exit code %ld\n", (long)tret); err = pthread_join(tid2, &tret);
if (err != )
{
err_exit(err, "can't join with thread 2");
}
printf("thread 2 exit code %ld\n", (long)tret); return ;
}

 

./a.out
thread start
thread push complete
cleanup: thread second handler
cleanup: thread first handler
thread start
thread push complete
thread exit code
thread exit code
     可知如果线程是通过它的启动例程中返回而终止的话,它的清理处理程序就不会被调用。
 
     在默认情况下,线程的终止状态会一直保存到对该线程调用pthread_join,如果该线程已经被分离,则底层的资源可以在线程终止时立即被收回,不用再调用pthread_join,且再调用pthread_join会出错。
#include <pthread.h>
int pthread_detach(pthread_t tid);

APUE学习之多线程编程(一):线程的创建和销毁的更多相关文章

  1. APUE学习之多线程编程(三):线程属性、同步属性

    一.线程属性      可以使用pthread_attr_t结构修改线程默认属性,并这些属性和创建的线程练习起来,可以使用pthread_att_init函数初始化pthread_attr_t结构,调 ...

  2. APUE学习之多线程编程(二):线程同步

         为了保证临界资源的安全性和可靠性,线程不得不使用锁,同一时间只允许一个或几个线程访问变量.常用的锁有互斥量,读写锁,条件变量           一.互斥量      互斥量是用pthrea ...

  3. .NET面试题解析(07)-多线程编程与线程同步

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实是很多的,比如多线程编程.线程上下文.异步编程.线程同步构造.GUI的跨线程访问等等, ...

  4. .NET面试题解析(07)-多线程编程与线程同步 (转)

    http://www.cnblogs.com/anding/p/5301754.html 系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实 ...

  5. Linux 系统编程 学习:09-线程:线程的创建、回收与取消

    Linux 系统编程 学习:09-线程:线程的创建.回收与取消 背景 我们在此之前完成了 有关进程的学习.从这一讲开始我们学习线程. 完全的开发可以参考:<多线程编程指南> 在Linux ...

  6. vc 基于对话框多线程编程实例——线程之间的通信

     vc基于对话框多线程编程实例——线程之间的通信 实例:

  7. Python中的多线程编程,线程安全与锁(二)

    在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...

  8. Python中的多线程编程,线程安全与锁(一)

    1. 多线程编程与线程安全相关重要概念 在我的上篇博文 聊聊Python中的GIL 中,我们熟悉了几个特别重要的概念:GIL,线程,进程, 线程安全,原子操作. 以下是简单回顾,详细介绍请直接看聊聊P ...

  9. C#多线程编程实例 线程与窗体交互

    C#多线程编程实例 线程与窗体交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = ]; public ...

随机推荐

  1. PHP安全之Web攻击

    一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...

  2. 查看Job执行的历史记录

    SQL Server将Job的信息存放在msdb中,Schema是dbo,表名以“sysjob”开头. 一,基础表 1, 查看Job和Step,Step_ID 是从1 开始的. select j.jo ...

  3. LLBL Gen Pro 5.0 企业应用开发入门

    Solutions Design 公司于2016年5月发布了LLBL Gen Pro 5.0,这个新版本的发布出乎于我的意料.我的猜想是从4.2升级到4.5,再升级5.x版本,主版本号的变更会给原有客 ...

  4. Git(远程仓库:git@oschina)-V2.0

    1.注册git@osc(也就是“码云”) 这里会提示注册密码==push密码,反正一定要记住的东西.   2.安装git 这里要设置个人信息 git config --list //查看git信息 g ...

  5. How to debug .NET Core RC2 app with Visual Studio Code on Windows?

    Simone Chiaretta (http://codeclimber.net.nz/archive/2016/05/20/How-to-debug-NET-Core-RC2-app-with-Vi ...

  6. php中会话保持 session 与cooker

    会话保持 1.session Session:在计算机中,尤其是在网络应用中,称为"会话控制".Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 ...

  7. Android面试一天一题(1Day)

    写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...

  8. 你真的会玩SQL吗?玩爆你的数据报表之存储过程编写(上)

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  9. jQuery2.x源码解析(构建篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 笔者阅读了园友艾伦 Aaron的系列博客< ...

  10. ASP.NET Core 中文文档 第三章 原理(14)服务器

    原文:Servers 作者:Steve Smith 翻译:谢炀(Kiler) 校对:许登洋(Seay).姚阿勇(Dr.Yao) ASP.NET Core 已完全从承载应用程序的 Web 服务器环境中分 ...