当前位置:网站首页>2. Create a thread
2. Create a thread
2022-07-18 19:56:00 【123_ YQH】
Create thread
1. Overview of creating threads
In general ,main The thread on which the function is located is called the main thread (main Threads ), The remaining threads created are called Sub thread .
In the program There is only one process by default ,fork() function After calling , There is 2 Threads in progress
In the program There is only one thread by default (main Threads , The main thread ),pthread_create() Function call , There is 2 Threads .
2. Create thread api function
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
- function : Create a child thread
- Parameters :
- thread: Out parameter , After the thread is created successfully , The thread of a child thread ID Is written to this variable .
- attr : Set thread properties , The default value is generally used ,NULL
- start_routine : A function pointer , This function is the logical code that the sub thread needs to process
- arg : Use... For the third parameter , The ginseng
- Return value :
success :0
Failure : Return error number . This error number is the same as before errno Not quite the same. .
Get information about the error number : char * strerror(int errnum);
3. Case study —— Create a thread
①
stay vscode Created in pthread_create.c file . The attached code is as follows :
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
//void* It's a universal pointer , The return value and parameters of the callback function are function pointers
void* callback(void *arg) {
printf(" Sub thread ");
return NULL;
}
int main() {
pthread_t tid; // The thread number used to receive the created sub thread
// Create a child thread
int ret = pthread_create(&tid, NULL, callback, NULL);
//ret = 0 Create success ,!= Print error number
if (ret != 0) {
char *errstr = strerror(ret);
printf("error : %s\n", errstr);
}
for (int i = 0; i < 5; ++i) {
printf("%d\n", i);
}
return 0; //exit(0), Withdraw from the process
}
stay xshell Input in gcc pthread_create.c -o p -lpthread Create executable p.( Because threads are not standard system libraries , Need to add -lpthread Represents the specified library pthread).
Input ./p 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-InNzveP3-1657788261594)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657787590806.png)]](/img/55/7d76b5881f2eb0a7784a317a7fdbc0.png)
②
We found that the code in the sub thread did not execute , This is because The sub thread has not been created or has not been executed yet , The main thread runs Yes return 0 Quit the whole process , As a result, the sub thread did not have time to execute .
We make the following changes , stay return 0 And before sleep(1), Let the main thread sleep 1 second , Allow time to execute child threads .
// ditto
for (int i = 0; i < 5; ++i) {
printf("%d\n", i);
}
sleep(1);
return 0; //exit(0), Withdraw from the process
}
![[ 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-ufTeXtWD-1657788261595)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657787882434.png)]](/img/3e/0146a14caf0ce079a5949bbede73bf.png)
It can be seen that the contents of the sub thread have been successfully executed .
边栏推荐
- 北京现代20周年:精简产品布局,力争2025年实现52万辆
- Horizon 8 测试环境部署(5): UAG 部署及负载均衡配置-1
- FIFO signal simulation of internal IP core based on FPGA
- Mina中的树结构
- 基于FPGA的内部IP核fifo信号仿真
- 11.滑动窗口的最大值——重要结构双端队列
- leetcode 43. 字符串相乘 (字符串+模拟)
- Unsafe service permissions cooperate with MSF to raise rights
- Chapter I FPGA digital signal processing_ Digital mixing (NCO and DDS)
- Vivado ROM IP核
猜你喜欢

Mina中的支付交易snark

11.滑动窗口的最大值——重要结构双端队列

使用基于ImageNet和VGG16模型训练可识别石头,剪刀,布的深度神经网络

Orm déchiqueté à la main (générique + annotation + réflexion)

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

【深度学习】线上租用设备平台体验以及踩过的坑(非广告)

安利一款笔记工具---Obsidian

BUCK 电路PSIM仿真模型搭建之三 (数字化差分方程的仿真)

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

Chapter I FPGA digital signal processing_ Digital mixing (NCO and DDS)
随机推荐
Open source! Hong Kong Chinese, MIT and Fudan put forward the first RNA cornerstone model
【深度学习】线上租用设备平台体验以及踩过的坑(非广告)
Film knowledge atlas and the construction of a template based question and answer system
Day 4 training
Web. Set the file upload size limit and request data stream size settings in config.
分享不停,踏浪前行 | 开发者说·DTalk 年中鉴赏
创业公司如何制定员工股份和股权的分配
How do startups make the distribution of employee shares and equity
Common functions of JMeter - parametric introduction
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,
Android day 27: database one
SQL optimization (V): table connection
Format output text in CAD
MQ brief introduction
【读书会第13期】+FFmpeg项目组成
Rizomuv exhibition UV usage notes
股权分配协议 (2)
12.单调栈——解决接雨水和柱状图中的最大矩形等问题
老外还停留在20年前
马斯克76岁父亲与继女生子,华强北又出一个芯片IPO,原蚂蚁副总投身AI制药,今日更多大新闻在此...