当前位置:网站首页>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);
}边栏推荐
- How to obtain the intersection / subtraction / Union of two sets by MySQL
- 【Unity入门计划】基本概念-GameObject&Components
- 轮询、中断、DMA和通道
- While (~scanf ("%d", & n)) is equivalent to while (scanf ("%d", & n)! =eof)
- 7.24 simulation summary
- CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!
- Using one stack to sort another stack
- Didi - dispatching
- P1086 [noip2004 popularization group question 2] peanut picking
- [software testing] package resume from these points to improve the pass rate
猜你喜欢

Native form submission data

第十七届振兴杯计算机程序设计员(云计算平台运维与开发)决赛

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

Didi eta (estimate the travel time)

Problems during nanodet training: modulenotfounderror: no module named 'nanodet' solution
![[unity introduction program] basic concepts GameObject & components](/img/fc/7e0a6f057394a6fd4409a7bf518ba5.png)
[unity introduction program] basic concepts GameObject & components

Network file storage system (III) practice of fastdfs distributed file system

CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!

Leetcode (Sword finger offer) - 04. search in two-dimensional array
![[unity introduction program] basic concepts -2d rigid body 2D](/img/67/537e9935bb0f2539945032cadcf232.png)
[unity introduction program] basic concepts -2d rigid body 2D
随机推荐
大佬秋招面经
enq: HW – contention等待引起的故障分析
Niuke dynamic planning training
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
The value of integer a after bitwise negation (~) is - (a+1)
纳米数据足球数据,足球赛事比分,体育数据api,卡塔尔世界杯
UNIPRO multi terminal deployment to meet customers' diversified needs
2-6. Automatic acquisition
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
pom容易忽略的问题
[unity introduction program] basic concept - preform prefab
[ffmpeg] MP4 to YUV
机器学习入门详解(一):理解监督学习中的最大似然估计
efcore在Saas系统下多租户零脚本分表分库读写分离解决方案
A review of nature: gender differences in anxiety and depression - circuits and mechanisms
[paper notes] effective CNN architecture design guided by visualization
【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
Nano data, football data, football match scores, sports data API, Qatar world cup
Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
Learn when playing No 5 | human high quality examination, right here →