linux线程篇 (二) 线程的基本操作
| 线程 | 进程 | |
| 标识符 | 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线程篇 (二) 线程的基本操作的更多相关文章
- C#线程篇---解答线程之惑(2)
我们都知道,在这个行业,追求的就是用最少的时间学最多的知识,这是我写这个系列最想达到的目标,在最快的时间内,帮助更多的人学习更多的线程知识. 前一篇,讲述了线程基础,给大家铺垫了一个基础,这一篇着重介 ...
- linux c编程:线程互斥二 线程死锁
死锁就是不同的程序在运行时因为某种原因发生了阻塞,进而导致程序不能正常运行.阻塞程序的原因通常都是由于程序没有正确使用临界资源. 我们举个日常生活中的例子来比喻死锁.我们把马路上行驶的汽车比作运行着的 ...
- linux线程篇 (三) 线程的同步
1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...
- linux线程篇 (一) 线程的基本概念
--进程 一个正在执行的程序,资源分配的最小单位 进程中的事情需要按照一定顺序区执行的,但是如何在一个进程中让一些事情同时发生呢?子进程存在缺陷 --引进多线程 --线程:有时又称轻量级进程,程序执行 ...
- linux进程篇 (二) 进程的基本控制
2. 进程的基本操作 接口函数 #include <unistd.h> //创建子进程 pid_t fork(void); //结束子进程 void exit(int status); / ...
- 从零开始学Linux系统(二)之基本操作指令
ifconfigping ip地址帮助:ping -t ip地址ping -c 次数 ip地址ping -s 包的大小关机重启:shutdown -h now reboot清屏:clear == C ...
- Linux基础篇二:Bash shell(壳,命令解释器)介绍
shell执行方式: 第一:输入命令 (简单工作) 第二: 脚本 (适合大量工作) Bash shell 实际上的叫法是 GNU/Bash 如何查询呢: bash - version ...
- 【转】C#线程篇
C# 温故而知新: 线程篇(一) C# 温故而知新: 线程篇(二) C# 温故而知新:线程篇(三) C# 温故而知新: 线程篇(四)
- 羽夏看Win系统内核——进程线程篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
随机推荐
- Python初学者第七天 字符串及简单操作
7day 数据类型:字符串 1.定义 字符串是一个有序的字符的集合,用于储存和表示基本的文本信息.单.双.三引号之间的内容称之为字符串: a = ‘hello world!’ b = "你好 ...
- GetDIBits 提示堆栈损坏的解决办法
...... BITMAPINFOHEADER bi; bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bmpScreen.bmWidth; bi ...
- JAVA读取HDFS信息
uri填路径 public static void main(String[] args) throws IOException { String uri = "/user/WeiboAD/ ...
- 在IE浏览器输入测试servlet程序报:HTTP Status 404(The requested resource is not available)错
一.HTTP Status 404(The requested resource is not available)异常主要是路径错误或拼写错误造成的,请按以下步骤逐一排查: 1.未部署Web应用 2 ...
- Yii框架记录
Yii框架记录 Yii 结构 使用yii开发一段时间,发现自身知其形不知其意,重温了下yii,理解框架,也可以梳理自己的知识库,借鉴成长,阶段性总结如下: 模型 模型是MVC模式中的一部分,是表现业务 ...
- 优秀 Java 程序员写代码的风格
往 期 精 彩 推 荐 [1]Java Web技术经验总结 [2]15个顶级Java多线程面试题及答案,快来看看吧 [3]面试官最喜欢问的十道java面试题 [4]从零讲JAVA ,给你一条清晰 ...
- Hbase集群部署及shell操作
本文详述了Hbase集群的部署. 集群部署 1.将安装包上传到集群并解压 scp hbase-0.99.2-bin.tar.gz mini1:/root/apps/ tar -zxvf hbase-0 ...
- python UI自动化实战记录六:页面1用例编写
使用python自带的unittest测试框架,用例继承自unittest.TestCase类. 1 引入接口类和页面类 2 setUp函数中打开页面,定义接口对象 3 tearDown函数中关闭页面 ...
- oracle 通配符及regexp_count函数说明
通配符 通配符描述示例 %:匹配包含零个或更多字符的任意字符串.WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名. ...
- Mybatis 和Spring整合之mapper代理开发
F:\1ziliao\mybatis\代码 1.1 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...