当前位置:网站首页>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
边栏推荐
- OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?
- Mavros controls UAV to conduct binocular slam in gazebo environment
- R analysis visual practical data (flight \u education \u restaurant \u tenant \u change \u life \u safety)
- If the source code of the home page module is not separated ----- > nanny level source code analysis (1)
- canvas旋转绘图h5动画js特效
- Multi thread alternate output ab
- OpenGL第七章 基础光照
- 单片机通信数据延迟问题排查
- Lecturer paging query_ Instructor condition query with page
- Technology Pro strength evaluation: comprehensive PK of high-end massage chair market, who is worthy of the machine king?
猜你喜欢

Understand single chip microcomputer drive 8080lcd

Azure kubernates service update | improve development experience and efficiency

Canvas rotation drawing H5 animation JS effect

If not, use the code generator to generate a set of addition, deletion, modification and query (2)

Unity之数据持久化——Json

Iqoo 8 measured hands-on experience: return of the king, never high profile

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
![[safety science popularization] have you been accepted by social workers today?](/img/ac/a6d2aa48219d02d82240d2f5343f19.jpg)
[safety science popularization] have you been accepted by social workers today?

TweenMax五彩小球弹跳动画

Delete the watermark of the picture uploaded by CSDN
随机推荐
Opencv实现纵横比保持的图像缩放
618将至!全渠道开售,高价低配的OPPO Reno6能赢吗?
Technology Pro strength evaluation: comprehensive PK of high-end massage chair market, who is worthy of the machine king?
OpenGl第九章 光照贴图
Integrated MP code generator
一文搞懂单片机驱动8080LCD
2022 年 5 月产品大事记
SQL查询连续三天登录的用户
If not, use the code generator to generate a set of addition, deletion, modification and query (2)
RHEL7 切换字符编码为GBK
Rhel7 switch character encoding to GBK
突破中国品牌创新技术实力,TCL做对了什么?
postgresql 捕获函数中的异常
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
HikariPool-1 - Shutdown initiated... HikariPool-1 - Shutdown completed.
Tweenmax colorful ball bouncing animation
OpenGL错误指南
{dataSource-1} closing ... {dataSource-1} closed
If the source code of the home page module is not separated ----- > nanny level source code analysis (1)
OpenGL第十一章 多光源