当前位置:网站首页>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
边栏推荐
- Lvgl Chinese font production
- OpenGl第十章 投光物
- Canvas interactive star animation background JS special effect
- Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?
- OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?
- If no separation ----- > > login module nanny level source code analysis (0)
- window10安装keras
- TweenMax五彩小球弹跳动画
- js最常用的排序---手撕js系列
- Gd32 can sends no mailbox fault
猜你喜欢
![[safety science popularization] mining technology starts from the love story of a man of science and Engineering](/img/01/73376c133c33527e479685f8680238.jpg)
[safety science popularization] mining technology starts from the love story of a man of science and Engineering

CheckBox美化按钮选中样式

OpenGL第七章 基础光照

Oppo K9 tests "bundling sales" and consumers "earn" or "lose"?

名不副实的雅迪高端品牌VFLY,为何“不高端”?

基于SSM框架的连锁超市购物零售后台管理系统

Lvgl Chinese font production

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

基于SSM的考试项目管理系统

Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?
随机推荐
What has TCL done right to break through the technological strength of Chinese brand innovation?
OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?
[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)
rt-thread测试
Difference between idea open and import project
[safety science popularization] have you been accepted by social workers today?
SQL | some indicators of the game industry
Lecturer paging query_ Instructor condition query with page
OpenGl第十章 投光物
【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩
OPENSSL ASN.1, DER, PEM, X509
UML series articles (28) architecture modeling - collaboration
Windows10 installing keras
【ELT.ZIP】OpenHarmony啃论文俱乐部——数据高通量无损压缩方案
OpenGL Chapter 7 basic lighting
Disk quota exceeded
一文搞懂单片机驱动8080LCD
正则表达式
被“内卷”酸翻的OPPO Reno6
js最常用的排序---手撕js系列