当前位置:网站首页>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$
边栏推荐
- Stack awareness - stack overflow instance (ret2text)
- Lagrange interpolation
- Ease of fire test for silicone rubber glass fiber pipe en45545
- 服务端socket程序
- [real topic of the Blue Bridge Cup provincial tournament 35] scratch water reflection children's programming scratch programming explanation of the real topic of the Blue Bridge Cup provincial tournam
- 3de 3D model View ne voit pas comment ajuster
- C语言与Lua的交互(实践二)
- Let, with, apply, also, run
- Xticks function in MATLAB
- Accelerate the implementation of cloud native applications, and Yanrong yrcloudfile and Tianyi cloud have completed the Compatibility Certification
猜你喜欢
![[Oracle] is there a](/img/21/3c481be79a4f19b06a2a93ded4159f.png)
[Oracle] is there a "time" data type in oracle-- Research on Oracle data types

【蓝桥杯省赛真题35】Scratch水面倒影 少儿编程scratch编程蓝桥杯省赛真题讲解

Stack cognition - Introduction to heap

shamir

tcpserver开启多线程处理

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

Runmaide medical passed the listing hearing: it is expected that the loss will increase, with huoyunfei brothers holding about 33%

Bm95 points candy problem

Excess rlsp

Encryption crash, is there a future for Web3 games? In depth discussion of 5 papers
随机推荐
堆栈认知——栈溢出实例(ret2libc)
一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策
aws elastic beanstalk入门之简介
Stack cognition -- basic use of reverse IDA tools
[dataset] |bigdetection
Bm22 compare version number
Viewing technological changes through Huawei Corps (IV): interactive media (Music)
《MATLAB 神经网络43个案例分析》:第26章 LVQ神经网络的分类——乳腺肿瘤诊断
天天在都在谈的S3协议到底是什么?一文带你了解S3背后的故事
拦截器实现网页用户登陆
海恩法则和费曼学习法
3DE 三維模型視圖看不到怎麼調整
Algorithm -- maximum number after parity exchange (kotlin)
Postman association to complete interface automation test
搜索(集训)
How many items should the indoor intumescent fire retardant coating meet according to BS 476-21 fire resistance standard?
Reids面试题集合 数据结构+穿透雪崩+持久化+内存淘汰策略+数据库双写+哨兵
透过华为军团看科技之变(四):互动媒体(音乐)
Simulation Implementation of list
Stack awareness - stack overflow instance (ret2libc)