当前位置:网站首页>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
边栏推荐
猜你喜欢

HikariPool-1 - Shutdown initiated... HikariPool-1 - Shutdown completed.

three. JS cool technology background H5 animation

用Fragment实现图片简易浏览

thinkphp3.2.3反序列化利用链分析

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

canvas+svg线条粒子动画网页背景

GD32F4串口dma接收问题解决

Instructor add function_ Enable auto fill_ Instructor modification function

Unity's data persistence -- Jason

canvas绘图——如何把图形放置画布中心
随机推荐
JS to realize coritization
If the source code of the home page module is not separated ----- > nanny level source code analysis (1)
Iqoo 8 measured hands-on experience: return of the king, never high profile
多线程四部曲之NSThread
If not, use the code generator to generate a set of addition, deletion, modification and query (2)
Product milestones in May 2022
OpenGl第九章 光照贴图
MySQL learning notes: JSON nested array query
If there is no separation ----- > > log interpretation (3)
名不副实的雅迪高端品牌VFLY,为何“不高端”?
Azure Kubernates Service 更新|提升开发体验和效率
What has TCL done right to break through the technological strength of Chinese brand innovation?
three.js炫酷科技感背景h5动画
js点击太阳月亮切换白天黑夜js特效
Jscpcp L. collecting diamonds (thinking)
The dependent version number in the dependencymanagement in the POM project parent in the idea is red
RT thread test
LVGL中文字体制作
Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?
一文搞懂单片机驱动8080LCD