当前位置:网站首页>POSIX create terminate thread
POSIX create terminate thread
2022-06-21 17:58:00 【Marathon】
In this paper, the reference 《 The embedded Linux Development tutorial 》 and 《Linux/UNIX System programming manual 》.
Create thread
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
thread Used to point to the newly created thread ID;
attr Used to represent a property object that encapsulates various properties of a thread , If attr by NULL, New threads use the default properties ;
start_routine Is the name of the function called when the thread starts executing ;
arg Is the parameter start_routine Specify the parameters of the function .
Thread termination
The process can be terminated by directly calling exit()、 perform main() Medium return、 Or through some other thread of the process exit() To achieve . In any of the above cases , All threads will be terminated . If the main thread has no tasks to process after creating other threads , Then it should block the wait until all threads are finished , Or you should call pthread_exit(NULL).
call exit() The function terminates the entire process , And call pthread_exit() It will only cause the calling thread to terminate , At the same time, it executes at the top level of the created thread return The thread implicitly calls pthread_exit().
void pthread_exit(void *retval);
retval It's a void Pointer to type , You can treat the return value of a thread as pthread_exit() Parameters passed in , This value is also pthread_join() Treat as exit status . If the last thread of the process calls pthread_exit(), The process will return the value with the status 0 sign out .
Thread example
The main thread created 5 Threads , this 5 Threads execute concurrently with the main thread , After the main thread creates the thread, it calls pthread_exit() Function exit thread , Other threads are
Print the serial number of the current thread . When the main thread executes before other processes pthread_exit() when , The process will not exit , Only the last thread is completed , The process will exit .
[email protected]-virtual-machine:~/workspace/test$ cat sample.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid){
// Thread functions
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid); // Print the parameters corresponding to the thread
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
// Loop creation 5 Threads
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); // Create thread
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
printf("In main: exit!\n");
pthread_exit(NULL); // Main thread exit
}
Here are the results , The main thread in the program calls pthread_exit() Function does not terminate the entire process , But when the last thread calls pthread_exit() When the program finishes running .
[email protected]-virtual-machine:~/workspace/test$ gcc sample.c -o sample -[email protected]-virtual-machine:~/workspace/test$ ./sample
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
Hello World! It's me, thread #1!
In main: creating thread 2
In main: creating thread 3
Hello World! It's me, thread #2!
In main: creating thread 4
Hello World! It's me, thread #3!
In main: exit!
Hello World! It's me, thread #4!
[email protected]-virtual-machine:~/workspace/test$
边栏推荐
- C语言与Lua的交互(实践三)
- 鱼佬:电信客户流失预测赛方案!
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- EtherCAT igh主站控制埃斯顿伺服回零
- 数字藏品系统开发,NFT艺术品交易平台搭建
- 【蓝桥杯省赛真题35】Scratch水面倒影 少儿编程scratch编程蓝桥杯省赛真题讲解
- 【技术管理】集结号与亮剑团队
- 3de 3D model View ne voit pas comment ajuster
- Live broadcast platform development, live broadcast of each category and single example design display
- Accelerate the implementation of cloud native applications, and Yanrong yrcloudfile and Tianyi cloud have completed the Compatibility Certification
猜你喜欢

Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition

Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"

Interceptor to realize web user login

Vit is crazy, 10+ visual transformer model details

《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别

Stack cognition -- basic use of reverse IDA tools

Fishman: telecom customer churn prediction game scheme!

tcpserver开启多线程处理

C语言与Lua的交互(实践三)

加密大崩盘,Web3游戏到底还有没有未来?5篇论文深度探讨
随机推荐
vector的模拟实现
Chapter V operation bit and bit string
Preorder traversal of BM23 binary tree
How to write technical documents software engineering at Google
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
C language DLL Dynamic Link Library
Use of list
Simulation of vector
Stm32f1 and stm32subeide programming example - linear Hall effect sensor driver
solidity智能合约面试题
Simulation Implementation of list
我被变相降薪了
How to adjust 3DE 3D model view if you can't see it
企业高管收入杂谈
神经网络七十年:回顾与展望
从需求到开源,如何做到刮目相看?
RT-Thread 柿饼派M7 全志F133 ddr 运行xboot
Application architecture principles
PTA l3-032 questions about depth first search and reverse order pair should not be difficult (30 points)
Fishman: telecom customer churn prediction game scheme!