当前位置:网站首页>5. Thread separation
5. Thread separation
2022-07-18 19:56:00 【123_ YQH】
Thread separation
1. summary
In the last section we knew , After the thread ends, it also needs to be recycled , Otherwise, it will generate Zombie Threads . Before using Connection recycling , Other threads are required to pass pthread_join The way to recycle .
In this section, we introduce a simpler way , Main thread pass pthread_detach() Function will Child threads are marked as detached . When a separate thread terminates , Its resources are automatically released back to the system , There is no need for another thread to join the terminating thread .
Try Connect already Separate Threads of will lead to unknown results .
2. function API
#include <pthread.h>
int pthread_detach(pthread_t thread);
- function : Separate a thread . When the separated thread terminates , It will automatically release resources and return them to the system .
1. Cannot separate multiple times , Will produce unpredictable behavior .
2. Cannot connect to a thread that has been separated , Will report a mistake .
- Parameters : The number of threads that need to be separated ID
- Return value :
success :0
Failure : Return error number
3. Case study —— The main thread marks the child threads as detached
①
Marking child threads as detached is simple , You only need to get its thread when you create a child thread ID, And then call pthread_detach( Sub thread ID) that will do . The code is as follows :
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
printf("chid thread id : %ld\n", pthread_self());
return NULL;
}
int main() {
// Create a child thread
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error1 : %s\n", errstr);
}
// Output the of main thread and sub thread id
printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
// Set child thread separation , After the sub thread is separated , When the child thread ends, the corresponding resources do not need to be released by the main thread
ret = pthread_detach(tid);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error2 : %s\n", errstr);
}
pthread_exit(NULL);
return 0;
}
stay xshell Input in gcc pthread_detach.c -o a.out -lpthread Generate executable files a.out.
Then input ./a.out Execution procedure , The results are as follows :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-yzW2rsd2-1657802699938)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657802383865.png)]](/img/59/e29fb02bbf26b211564d93e7f1d0fc.png)
It can be seen that , The execution result does not show whether the child thread is recycled .
②
We connect the separated sub threads ,pthread_join, If there is an error, it means that the sub thread has been successfully separated . Add code :
// On the same
// Set child thread separation , After the sub thread is separated , When the child thread ends, the corresponding resources do not need to be released by the main thread
ret = pthread_detach(tid);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error2 : %s\n", errstr);
}
// After setting separation , Connect separate child threads pthread_join()
ret = pthread_join(tid, NULL);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error3 : %s\n", errstr);
}
pthread_exit(NULL);
return 0;
}
After compiling and running, the following results are produced :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-JQ1scZUS-1657802699939)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657802678359.png)]](/img/89/e9e6d98b0c7a3bc0b2241f225f5ae0.png)
There was a mistake , Description separation successful .
边栏推荐
- Day 4 training
- 7.9 hash table (hash table)
- 对话印奇:我们所坚持的不会改变,旷视跳出企业科研“周期律”
- Dark horse programmer - software testing -15 stage 3- function testing 146-173 case review summary, tpshop project practice, state transition method cases, flow chart, business process testing, relati
- Unity DOTween插件和iTween插件使用(笔记)
- 5.线程分离
- Tear ORM by hand (generic + annotation + reflection)
- [MySQL project practical optimization] convert multiple rows of data into the same row and multiple columns for display
- Qt中的单例模式:实现一个单例的界面类
- Horizon 8 测试环境部署(8): App Volumes Managers 负载均衡配置
猜你喜欢

安卓 Day 26 :数据库Two

The 20th anniversary of Beijing Hyundai: Streamline product layout and strive to achieve 520000 vehicles by 2025

Dark horse programmer - software testing -14 stage 3- function testing -66-77 Zen introduction, product manager uses Zen, super administrator uses Zen, super administrator modifies security strategy,

不安全的服务权限配合MSF提权

Android day 26: database two

基于FPGA的内部IP核fifo信号仿真

2.创建线程
![[MySQL project practical optimization] convert multiple rows of data into the same row and multiple columns for display](/img/e8/442e2a76296a0a58639640f4d68bee.png)
[MySQL project practical optimization] convert multiple rows of data into the same row and multiple columns for display

Interview micro service

HCIA总结
随机推荐
6. Violent recursion to dynamic planning
vivado之COE文件使用方法
HCIA summary
Mental Poker Revisited学习笔记
【深度学习】线上租用设备平台体验以及踩过的坑(非广告)
Logs issue 2022/07/16 | Li Jia, Hong Kong University of science and technology: Rethinking graph anomaly detection - what kind of graph neural network do we need?
[codetool] file comparison beyond compare introduction
Open source! Hong Kong Chinese, MIT and Fudan put forward the first RNA cornerstone model
Unity mouse controls the capture and movement of 3D objects and UI (notes)
公司股权结构顶层设计方案(案例)
简单介绍 MySQL 四类日志
第一章 FPGA数字信号处理_数字混频(NCO与DDS)
Equity distribution agreement (1)
RizomUV展UV使用记要
5道String高频面试题,拿捏String底层原理!
Dark horse programmer - software testing -14 stage 3- function testing -66-77 Zen introduction, product manager uses Zen, super administrator uses Zen, super administrator modifies security strategy,
8. Island issue
HCIA总结
leetcode 45. Jump Game II (dp)
1.3.2-SQL注入-SQL盲注-时间盲注