1. Introduction
Pthread is part of the POSIX multithreaded development framework. It is a cross-platform multithreaded solution provided by the C language. Because of its in iOS programming, the operation is more troublesome, generally not, here introduced only as understanding.
Use of 2.pthread
Create a pthread with the following function, which is usually _t/ref at the end of the type in C and does not require the use of *:
const pthread_attr_t * __restrict, void *(*)(void *), void * __restrict);
Parameters:
1. pthread_t * __restrict 线程代号的地址 2. onst pthread_attr_t * __restrict 线程的属性 3. 调用函数的指针 - void *(*)(void *) - 返回值 (函数指针)(参数) - void * 和 OC 中的 id 是等价的 4. void * __restrict :传递给该函数的参数
return value:
- If it's 0, that's correct.
- If it is non-0, it indicates an error code
3. Code Combat:
#import "ViewController.h" #import <pthread.h> @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];NSString*STR = @"MR"; pthread_t thread; Pthread_create (&thread,NULL, &demo, (__bridgevoid*) (str));void* Demo (void* params) {NSString*str = (__bridgeNSString*) (params); for(inti =0; I < -; i++) {NSLog(@"%@", str); }return NULL;}@end
__bridge (bridge Connection)
- In ARC development, if you are designing to convert to the same data type in C, you need to use the __bridge "bridge"
- In OC, if ARC is developed, the compiler will automatically add retain, release, and autorelease to the code structure at compile time.
- ARC is only responsible for the OC part of the code, not responsible for C code, if the C language frame appears retain/create/copy word function, all need to release
Run Results
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Multithreading---pthread