当前位置:网站首页>One of C language multithreading programming
One of C language multithreading programming
2022-07-25 07:59:00 【Starry sky_ MAX】
stay C You can use pthread.h Of API To create threads ,pthread.h accord with POSIX standard , Means that you can Unix and Linux Run under ,Windows NT It also provides corresponding support
Create multithreading :
#include<stdio.h>
#include<pthread.h>
void *thread_func(void *args)
{
printf("This is thread_func");
return 0;
}
int main()
{
pthread_t thread;
if(pthread_create(&thread,NULL,thread_func,NULL))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
pthread_exit(NULL);
}About pthread_create function , When successful, the return is 0, When you fail, you return 1
The first parameter is a pointer to the thread identifier
The second parameter is thread property
The third parameter is the starting address of the function
The fourth parameter is the parameter of the running function , With the fourth parameter, you can pass parameters into the function when creating a thread
Pass parameters into the created thread :
#include<stdio.h>
#include<pthread.h>
void *thread_func(void *parameter)
{
printf("This is thread_func,parameter:%s",parameter);
return 0;
}
int main()
{
pthread_t thread;
char* parameter="ok";
if(pthread_create(&thread,NULL,thread_func,(void *)parameter))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
pthread_exit(NULL);
}Creating a thread pool ( Pass in parameters when creating ):
#include<stdio.h>
#include<pthread.h>
#define THREAD_NUM 5
void *thread_func(void *parameter)
{
printf("This is thread_func,parameter:%d\n",parameter);
return 0;
}
int main()
{
pthread_t threads[THREAD_NUM];
for(int i=0;i<THREAD_NUM;i++)
{
if(pthread_create(&threads[i],NULL,thread_func,(void*)i))
{
printf("creat thread fail\n");
}
else
{
printf("creat thread successfull\n");
}
}
pthread_exit(NULL);
}边栏推荐
- [unity introduction program] basic concepts - 2D collider collider 2D
- 【Unity入门计划】基本概念-触发器 Trigger
- 文件详细操作
- MathWorks has been in China for 15 years. What are the secrets of continuous innovation?
- Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once
- Install homebrew, NVM and verdaccio to build a private NPM warehouse
- enq: HW – contention等待引起的故障分析
- [QNX hypervisor 2.2 user manual]9.3 CPU
- 【Unity入门计划】基本概念-预制件 Prefab
- Pricing is arbitrary, products are difficult to distinguish between true and false, and platforms are running away. Will the Tibetan market continue to be popular?
猜你喜欢
![[dynamic programming] - Knapsack model](/img/0d/c467e70457495f130ec217660cbea7.png)
[dynamic programming] - Knapsack model

app耗电量测试

batchnorm 和layernorm的区别

File details

交叉熵计算公式
Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once

Science: listening to music can really relieve pain. Chinese scientists reveal the neural mechanism behind it

【论文笔记】Next-ViT: Next Generation Vision Transformer for Efficient Deployment in Realistic Industrial

oracle 触发器创建

Google AI can't understand the comments of netizens, and the wrong meaning will be as high as 30%. Netizens: you don't understand my stem
随机推荐
When deep learning makes data sets, it specifies how many frames to extract an image from the long video to the specified file path
J1 common DOS commands (P25)
【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
Efcore's solution of multi tenant zero script, table and database read-write separation under SaaS system
[unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
滴滴 - dispatching
[unity introduction program] basic concepts - 2D collider collider 2D
Acnet: asymmetric convolution for image hypersegmentation (with implementation code)
【Unity入门计划】基本概念-预制件 Prefab
Dijkstra序列(暑假每日一题 5)
If Debian infringes the rust trademark, will it be exempted by compromising and renaming?
Network file storage system (II) practical operation of Minio distributed file system
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
[QNX Hypervisor 2.2用户手册]9.3 cpu
What products and funds should novices invest in first?
【Unity入门计划】制作我的第一个小游戏
设计一个有getMin功能的栈
Vscode remote connection server, switch to go version
webflux默认io线程数
文件详细操作