当前位置:网站首页>Pthread in the multithreaded Trilogy
Pthread in the multithreaded Trilogy
2022-06-11 03:35:00 【chabuduoxs】
pthread brief introduction
pthread It is a set of general multithreaded API, Can be in Unix / Linux / Windows And other systems are used across platforms , Use C Language writing , Programmers need to manage the life cycle of threads themselves , It is difficult to use , We are iOS There is little use of... In development pthread, But you can still learn about it .
POSIX Threads ( English :POSIX Threads, Often abbreviated by Pthreads) yes POSIX Thread standard for , Defines a set of methods for creating and manipulating threads API.
Realization POSIX Thread standard libraries are often referred to as Pthreads, Commonly used in Unix-like POSIX System , Such as Linux、Solaris. however Microsoft Windows There are also implementations on , For example, direct use Windows API Third party libraries implemented pthreads-w32; And the use of Windows Of SFU/SUA Subsystem , You can use part of the native software provided by Microsoft POSIX API.
pthread Use
First
//
// ViewController.m
// pthread
//
// Created by Almost sir on 2022/6/6.
//
#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@end
@implementation ViewController
void *run(void* param) {
for (NSInteger i = 0; i < 5; i++) {
NSLog(@"%ld-> %@", i, [NSThread currentThread]);
}
return NULL;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
pthread_t myThread;
int res = pthread_create(&myThread, NULL, run, NULL);
if (res == 0) {
NSLog(@" Thread created successfully !");
}
// Release all resources after the thread ends
pthread_detach(myThread);
NSLog(@"%@", [NSThread currentThread]);
}
@end

It can be seen that the thread here is running at the same time as the main thread , The meaning of the four parameters :
&myThread Is a thread object , Pointer to thread identifier
The second is Thread properties , The default is NULL
Third run Represents a pointer to a function , Newly created thread from run The function address starts running
Fourth default by NULL, If the above function requires parameters , Pass the address into
Some other uses
pthread_create() Create a thread
pthread_exit() Terminate the current thread
pthread_cancel() Interrupt another thread
pthread_join() Block current thread , Until the end of another thread
pthread_attr_init() Initialize the properties of the thread
pthread_attr_setdetachstate() Set the out of state property ( Determines whether the thread can be combined at termination )
pthread_attr_getdetachstate() Get out of state properties
pthread_attr_destroy() Delete thread properties
pthread_kill() Send a signal to the thread
边栏推荐
- js点击太阳月亮切换白天黑夜js特效
- js顶部图标菜单点击切换背景色js特效
- Azure kubernates service update | improve development experience and efficiency
- WEB上传文件预览
- 联易融一面(已过)
- Canvas drawing -- how to place the drawing in the center of the canvas
- Delete the watermark of the picture uploaded by CSDN
- [safety science popularization] have you been accepted by social workers today?
- If no separation ----- > > login module nanny level source code analysis (0)
- Canvas interactive star animation background JS special effect
猜你喜欢

B_ QuRT_ User_ Guide(18)

潮玩力真火力!年轻人第一台巨幕影院?酷开电视Max 86“庞然来袭

ThoughtWorks. QRcode full-featured generator

How should Xiaobai start the Amazon self support evaluation?

Azure kubernates service update | improve development experience and efficiency

PostgreSQL source code learning (22) - fault recovery ③ - transaction log registration

618将至!全渠道开售,高价低配的OPPO Reno6能赢吗?

【ELT.ZIP】OpenHarmony啃论文俱乐部——多层存储分级数据压缩

three.js炫酷科技感背景h5动画

thinkphp3.2.3反序列化利用链分析
随机推荐
Integrated MP code generator
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
单片机通信数据延迟问题排查
Thinkphp3.2.3 deserialization using chain analysis
OpenGL第十一章 多光源
SQL查询连续三天登录的用户
canvas+svg线条粒子动画网页背景
Reasons why Chinese comments cannot be written in XML
PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level
GD32 can发送报no mailbox 故障
Azure Kubernates Service 更新|提升开发体验和效率
Right click PowerShell here function add
一文搞懂单片机驱动8080LCD
OpenGL错误指南
SSL interaction process
Disk quota exceeded
OpenGL第七章 基础光照
SQL | some indicators of the game industry
/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter
Xu Li 618, how can Suning fight this hard battle?