线程 进程
标识符 pthread_t pid_t
获取ID pthread_self() getpid()
创建 pthread_create() fork
销毁 pthread_exit() exit()
等待  pthread_join() wait()
 取消 pthread_cancel()  
信号发送 pthread_kill() kill() raise() alarm()
信号处理 signal signal
信号屏蔽 pthread_sigmask()  
线程清除 pthread_cleanup_push/pop  

1.线程的创建

#include <pthread.h>

pthread_t pthread_self(void);    //返回自己的线程id

int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg); //pthread_t *thread 新线程的id指针,注意是指针, pthread_t ntid; 这里就是 &tid
//const pthread_attr_t *attr, 新线程属性,这里暂为NULL
//void *(*start_routine) (void *), 新线程的函数入口
/*
* 描述:这是一个函数指针作为入口函数
* 参数:void * 指针类型
* 返回值为 void*
* (*start_routine) 这是一个指针变量,它指向一个函数,
因此在实参里本应该是&thread_fun
但是因为 函数名编译后,本身就是指针,所以可以隐去&
*/ //实例:pthread_creat(&ntid,NULL,&thread_fun,"我是给thread_fun的参数");
// void *thread_fun(void *arg){} void pthread_exit(void *value_ptr);

2.线程的终止

单个线程的安全退出 ()
() 从启动线程中返回,返回值时线程的退出码
() 线程可以被同一进程中的其他线程取消
() 线程调用pthread_exit(void *rval),rval 是退出码 void pthread_exit(void *value_ptr);

3.线程的链接

#include <pthread.h>

int pthread_join(pthread_t thread, void **value_ptr);
//该函数的线程会一直阻塞,直到 第一个参数的线程退出后,继续运行,第一个参数的线程退出码会被保存到第二个参数里,
//return 成功0 失败错误吗 //调用pthread_join 会让指定的线程处于分离状态,如果该线程已经是分离状态,那就会调用失败。 //
int pthread_detach(pthread_t thread); //线程分离,可以分离自己

4.线程取消

//取消线程
int pthread_cancel(pthread_t thread); ​//取消状态
int pthread_setcancelstate(int state, int *oldstate);
//PTHREAD_CANCEL_ENABLE 允许取消
//PTHREAD_CANCEL_DISABLE 不允许取消​ //取消类型
int pthread_setcanceltype(int type, int *oldtype);
//PTHREAD_CANCEL_DEFERRED 延迟取消
//PTHREAD_CANCEL_ASYNCHRONOUS 立即取消 ​//取消点 如果是延时取消,那么在每一个取消点都会检查是否取消

5.线程信号

//1.信号的发送
int pthread_kill(pthread_t thread, int sig); //向线程发送信号 //return
// [ESRCH] thread is an invalid thread ID.
// [EINVAL] sig is an invalid or unsupported signal number.
//[ENOTSUP] thread was not created by pthread_create() and does not support being killed with
pthread_kill() //信号的大部分操作是终止进程,所以要对信号作出正确的处理。
//2.信号的处理
int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact); struct sigaction {
union __sigaction_u __sigaction_u; /* signal handler */
sigset_t sa_mask; /* signal mask to apply */
int sa_flags; /* see signal options below */
}; union __sigaction_u {
void (*__sa_handler)(int);
void (*__sa_sigaction)(int, siginfo_t *,
void *);
};
//3.信号的屏蔽
int pthread_sigmask(int how, const sigset_t * restrict set, sigset_t * restrict oset);

6.进程的清除

//线程可以注册多个清理程序,入栈的形式 ,所以执行顺序和注册顺序相反

//注册清理程序
void pthread_cleanup_push(void (*cleanup_routine)(void *), void *arg); //销毁清理程序
void pthread_cleanup_pop(int execute); //响应方式
//1.pthreat_exit
//2.pthread_cancel
//3.调用 void pthread_cleanup_pop(int execute); 非零参数

linux线程篇 (二) 线程的基本操作的更多相关文章

  1. C#线程篇---解答线程之惑(2)

    我们都知道,在这个行业,追求的就是用最少的时间学最多的知识,这是我写这个系列最想达到的目标,在最快的时间内,帮助更多的人学习更多的线程知识. 前一篇,讲述了线程基础,给大家铺垫了一个基础,这一篇着重介 ...

  2. linux c编程:线程互斥二 线程死锁

    死锁就是不同的程序在运行时因为某种原因发生了阻塞,进而导致程序不能正常运行.阻塞程序的原因通常都是由于程序没有正确使用临界资源. 我们举个日常生活中的例子来比喻死锁.我们把马路上行驶的汽车比作运行着的 ...

  3. linux线程篇 (三) 线程的同步

    1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...

  4. linux线程篇 (一) 线程的基本概念

    --进程 一个正在执行的程序,资源分配的最小单位 进程中的事情需要按照一定顺序区执行的,但是如何在一个进程中让一些事情同时发生呢?子进程存在缺陷 --引进多线程 --线程:有时又称轻量级进程,程序执行 ...

  5. linux进程篇 (二) 进程的基本控制

    2. 进程的基本操作 接口函数 #include <unistd.h> //创建子进程 pid_t fork(void); //结束子进程 void exit(int status); / ...

  6. 从零开始学Linux系统(二)之基本操作指令

    ifconfigping ip地址帮助:ping -t ip地址ping -c 次数 ip地址ping -s 包的大小关机重启:shutdown -h now reboot清屏:clear  == C ...

  7. Linux基础篇二:Bash shell(壳,命令解释器)介绍

    shell执行方式: 第一:输入命令  (简单工作) 第二:   脚本  (适合大量工作) Bash shell 实际上的叫法是  GNU/Bash    如何查询呢:  bash - version ...

  8. 【转】C#线程篇

    C# 温故而知新: 线程篇(一) C# 温故而知新: 线程篇(二) C# 温故而知新:线程篇(三) C# 温故而知新: 线程篇(四)

  9. 羽夏看Win系统内核——进程线程篇

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...

随机推荐

  1. angular2 文件上传

    ng2-file-upload文件上传 1.安装ng2-file-upload模块 npm install ng2-file-upload --save 2.如果使用systemjs打包,需要在配置s ...

  2. c++新标准的一个问题

    显示转换运算符存在多个兼容版本的时候,explicit 关键字无效,编译器默认会选择那个兼容版本进行转换,而不是报错. 测试环境:gcc4.8.1 示例代码: class plebe { privat ...

  3. Webpack笔记(一)——从这里入门Webpack

    准备了挺久,一直想要好好深入了解一下Webpack,之前一直嫌弃Webpack麻烦,偏向于Parcel这种零配置的模块打包工具一些,但是实际上还是Webpack比较靠谱,并且Webpack功能更加强大 ...

  4. Hibernate 基于主键的一对一关联关系随手记

    @Test public void testSave() { Boss boss = new Boss(); boss.setName("A-老板"); Company compa ...

  5. Linux修改权限命令chmod用法详解

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

  6. CPU与GPU区别大揭秘

    http://blog.csdn.net/xiaolang85/article/details/51500340 有网友在网上提问:“为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚至破解 ...

  7. EOJ Monthly 2019.1 唐纳德先生与这真的是签到题吗 【数学+暴力+multiset】

    传送门:https://acm.ecnu.edu.cn/contest/126/ C. 唐纳德先生与这真的是签到题吗 单测试点时限: 6.0 秒 内存限制: 1024 MB 唐纳德先生在出月赛的过程中 ...

  8. EasyUI使用之鼠标双击事件

    easyui鼠标双击事件,使用 onDblClickRow(index, row) 事件,在用户双击一行的时候触发,参数包括: index:点击的行的索引值,该索引值从0开始. row:对应于点击行的 ...

  9. 使用Spring实现AOP(XML+注解)

    一.Spring对AOP的支持 AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一,每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行 ...

  10. 【luogu P3393 逃离僵尸岛】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3393 被占领的点可以先连在一个点上然后只需要对这一个点bfs一遍就可以求所有的危险点 #include &l ...