linux线程学习初步02

  1. 杀死线程的函数
int pthread_cancel(pthread_t thread);
参数介绍:需要输入的tid
返回值:识别返回 errno成功返回 0
被杀死的线程,退出状态值为一个
#define PTHREAD_CANCELED((void *)-1)

代码案例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>void* thr(void *arg) {while(1) {printf("I am a common thread\n");sleep(1);}return 100;
}int main() {pthread_t ptid;pthread_create(&ptid, NULL, thr, NULL);sleep(5);void *ret;pthread_cancel(ptid);int num = pthread_join(ptid, &ret);printf("num = %d, ret = %d\n",num , (int)ret);return 0;
}
``2. 线程分离函数```cppint pthread_detach(pthread_t thread);

使用了线程分离函数, 该线程不用再被
pthread_join函数回收资源

代码案例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>void* thr(void *arg) {printf("I am a common thread  1\n");sleep(3);printf("I am a common thread  1-\n");return (void *)100;
}int main() {pthread_t ptid;pthread_create(&ptid, NULL, thr, NULL);pthread_detach(ptid);sleep(4);int ret = pthread_join(ptid, NULL);printf("ret = %d error is %s\n",ret, strerror(ret));return 0;
}
  1. 比较两个线程id是否相等
int pthread_equal(pthread_t t1, pthread_t t2);

线程id在进程内部是唯一的, 但是在整个操作系统中不是唯一的

  1. 线程属性设置
初始化线程属性
int pthread_attr_init(pthread_attr_t *attr);销毁线程属性
int pthread_attr_destroy(pthread_attr_t *attr);

设置线程分离

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
参数介绍:attr:init初始化的属性detachstate:PTHREAD_CREATE_DETACHEDThreads that are created using attr will be created in a detached state.PTHREAD_CREATE_JOINABLEThreads that are created using attr will be created in a joinable state.int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);

发现,设置成为线程分离后, 主线程如果先执行完了, 子线程没有机会执行
代码案例:

nclude <unistd.h>
#include <pthread.h>
#include <string.h>void* thr(void *arg) {sleep(2);printf("I am a common thread  1\n");return (void *)100;
}int main() {pthread_attr_t attr;pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);pthread_t ptid;pthread_create(&ptid, &attr, thr, NULL);printf("I am a main thread\n");pthread_attr_destroy(&attr);pthread_exit(NULL);// 如果不加这句话,自线程没有机会打印
}

getconf GNU_LIBPTHREAD_VERSION 查看版本

注意事项:
被join线程可能在join函数返回前,就释放完自己所有的内存资源,所以不应当回收线程栈中的值

malloc和mmap申请的内存可以被其他线程释放

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382499.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python的文件基本操作和文件指针

读写模式的基本操作 https://www.cnblogs.com/c-x-m/articles/7756498.html r,w,a r只读模式【默认模式&#xff0c;文件必须存在&#xff0c;不存在则抛出异常】w只写模式【不可读&#xff1b;不存在则创建&#xff1b;存在则清空内容】a之追加写模式【不可读&#xff1b;不…

python3 将unicode转中文

decrypted_str.encode(utf-8).decode(unicode_escape)

HTTP菜鸟教程速查手册

HTTP协议&#xff08;HyperText Transfer Protocol&#xff0c;超文本传输协议&#xff09;是因特网上应用最为广泛的一种网络传输协议&#xff0c;所有的WWW文件都必须遵守这个标准。 HTTP是一个基于TCP/IP通信协议来传递数据&#xff08;HTML 文件, 图片文件, 查询结果等&am…

mysql学习笔记01-创建数据库

创建数据库&#xff1a; 校验规则&#xff1a;是指表的排序规则和查询时候的规则 utf8_general_ci 支持中文&#xff0c; 且不区分大小写 utf8_bin 支持中文&#xff0c; 区分大小写 比如&#xff1a; create database db3 character set utf8 collate utf8_general_ci; &…

python的Web编程

首先看一下效果 完整代码 import socket from multiprocessing import ProcessHTML_ROOT_DIR ""def handle_client(client_socket):request_data client_socket.recv(1024)print("request data:", request_data)response_start_line "HTTP/1.0 20…

mysql 学习笔记 02创建表

表结构的创建 比如&#xff1a; create table userinfo (id int unsigned comment id号name varchar(60) comment 用户名password char(32),birthday date ) character set utf8 engine MyISAM;comment 表示注释的意思 不同的存储引擎&#xff0c;创建的表的文件不一样

mysql 学习笔记03 常用数据类型

数值类型&#xff1a; a. 整数类型&#xff1a; 注意事项&#xff1a; 举例&#xff1a;某个整型字段 &#xff0c;不为空&#xff0c;且有默认值 create table test (age int unisigned not null default 1);zerofill的使用 b. bit类型的使用 c.小数类型 小数类型占用…

VMware的虚拟机连不上网

1.如果你发现在VMware下运行的虚拟机无法连接网络&#xff0c;那下面的方法也许可以帮 到你。&#xff08;前提是你的物理网络是通的&#xff09; 第一步&#xff1a;在VMware界面下 单击“编辑“→”虚拟网络编辑器” 第二步&#xff1a;单击”更改设置” 获取权限&#xff…

python三国演义人物出场统计

完整代码 开源代码 统计三国演义人物高频次数 #!/usr/bin/env python # codingutf-8 #e10.4CalThreeKingdoms.py import jieba excludes {"来到","人马","领兵","将军","却说","荆州","二人","…

mysql 学习笔记03修改表以及其他操作

首先创建一张表 在现有表的结构上增加字段 alter table users add image varchar(100) not null defalut comment 图片路径;修改某个字段的长度 alter table users modify job vachar(60) not null comment 工作;删除某个字段 删除sex这个字段 alter table users drop se…

统计哈姆雷特文本中高频词的个数

统计哈姆雷特文本中高频词的个数 三国演义人物出场统计 开源代码 讲解视频 kouubuntu:~/python$ cat ClaHamlet.py #!/usr/bin/env python # codingutf-8#e10.1CalHamlet.py def getText():txt open("hamlet.txt", "r").read()txt txt.lower()for ch…

mysql 学习笔记04 insert与update语句

1.插入数据 注意事项&#xff1a; 字符和日期类型&#xff0c; 要用 单引号 括起来 insert into (), (), () 例如&#xff1a; insert into goods values(1, abc, 2.2), (2, def, 3.3);这种形式添加多条记录 insert 语句&#xff0c;如果没有指定字段名&#xff0c;则values …

PyCharm怎么关闭端口,解决端口占用问题

在进行web开发遇到这个问题&#xff01;

mysql 笔记05 select语句以及条件语句的使用

select语句 过滤重复语句&#xff08;distinct&#xff09; 举例&#xff1a; 查询学生的总分 select name, math English China as 总分 from students;在姓赵的学生总分基础上&#xff0c; 增加60%&#xff0c; select name, round((math English China) * 1.6, 2) as …

python3 与 Django 连接数据库:Error loading MySQLdb module: No module named 'MySQLdb'

解决方法&#xff1a;在 init.py 文件中添加以下代码即可。 import pymysql pymysql.install_as_MySQLdb()

mysql 学习笔记05 统计函数的相关使用

合计函数count&#xff0c; 统计多少条记录 统计共有多少学生 select count(*) from students;查询数学成绩大于等于90的学生数量 select count(*) from students where math > 90;查询总分超过235分的学生的数量 select count(*) from students where (English math Ch…

Unknown column '' in 'field list'

Unknown column ‘’ in ‘field list’ 解决办法 正确写法&#xff1a;cursor.execute("update book set name%s where id%d" % (name, int(id))) 错误写法&#xff1a;cursor.execute("update book set name%s where id%d" % (name, int(id)))你要获取字…

mysql学习笔记06分组语句的使用

group by 子句 对列进行分组 有两张表&#xff1a; 一张为部门表&#xff0c; 一张为员工表统计 每个部门的平均工资&#xff0c;与最高工资 select avg(salary), max(salary) from emp group by deptno;统计 每个部门的每个岗位的 平均工资与最低工资&#xff08;注意这里的…

Django学习笔记《一》初始化pycharm和mysql数据库及相关环境

真的不太好学啊&#xff01;记录一下笔记&#xff0c;要不有的东西老要查&#xff01; CSRF验证 防止伪造数据添加数据库&#xff0c;关闭方式 MIDDLEWARE [django.middleware.security.SecurityMiddleware,django.contrib.sessions.middleware.SessionMiddleware,django.m…