Retrospective function pointer
C function syntax: return value type function name (argument list){function body; } * /
void test(){
NSLog(@ "...");
}
/* Function pointer syntax: return value type (* function name)(argument list) */
void (*p)(); // This is a pointer variable named p that points to a function with no parameters and no return value
int (*p2)(); // this is a pointer variable named p2 that points to a function that has no argument and returns a value of type int
int (*p3)(int num); // this is a pointer variable named p3, which points to a function that takes an int argument and returns an int value
/* How to assign a pointer to a function? P = function name; Because the function name is the function address */
p = test;
//void* is a universal pointer to OC
Copy the code
The use of pthread
- Pthread opens child threads
- Before using pthreads, you must import the header file
#import <pthread.h>
- Before using pthreads, you must import the header file
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//NSLog(@" click in, create before %@",[NSThread currentThread])
//[self demo];
//C types that end in _t or _ref are C types
pthread_t p;
NSString *str = @"abc";
If &p is passed, the thread is pointed to by p. In other words, the child thread can be found by p
// Parameter 2: thread attribute (can set the size of memory used by the thread, usually NULL)
// Parameter 3: function name (the function that needs this thread to execute)
// Argument 4: function argument, NULL if no argument
// Arguments 3 and 4 mean: which function code is executed by the child thread and what arguments are passed to this function
int res = pthread_create(&p, NULL,test, (__bridge void *)(str));
// Bridge: a bridge that connects two types
// Why? Because OC objects are currently collected by the system management under ARC
// However, C objects are not managed by the system, so we need to bridge OC to C objects
// This means that the system will manage the object after it is converted to C
// The return value is int, indicating whether the thread was created successfully
// Return 0 for success, return other for failure
// The return value is actually an error identifier. Only 0 indicates that there is no error. Other numbers indicate different errors
For example, 1 indicates an XXX error, and 2 indicates an YYy error
if(res == 0) {NSLog(@" Created successfully");
}else{
NSLog(@" create failed");
}
//NSLog(@" create %@",[NSThread currentThread]);
}
void *test(void *num){
NSString *str = (__bridge NSString *)(num);
//NSLog(@"%p",num);
// NSLog(@"test %@",[NSThread currentThread]);
NSLog(@ "% @",str);
return NULL; } - (void)demo{
[NSThread currentThread] prints information about the currentThread object
// See which thread is executing the current code
Number ==1; =1 is the child thread */
NSLog(% @ "@" demo method[NSThread currentThread]);
}
Copy the code
The use of the NSThread
There are three creation methods
-
Method of construction
//NSThread is a thread object Arguments 1 and 2: Which method of which object is to be called by the opened child thread // Argument 3: Arguments to this method are passed nil if they exist NSThread *th1 = [[NSThread alloc] initWithTarget:self selector:@selector(demo:) object:@"hello"]; // To create it this way, be sure to start [th1 start]; Copy the code
PS: Need to start!!
This thread object can also be accessed outside of methods executed by the thread
-
Class method mode
// Parameter 1: which method // Parameter 2: which object // Parameter 3: method parameter // This method creates a new thread to execute a method on an object [NSThread detachNewThreadSelector:@selector(demo:) toTarget:self withObject:@"hello"]; Copy the code
Don’t need to start
The thread object is not accessible outside the invoked method
-
Classification (classification of NSObject)
// Parameter 1: which method to call // Parameter 2: method parameter [self performSelectorInBackground:@selector(demo:) withObject:@"abc"]; Copy the code
You don’t need to start
The thread object is not accessible outside the called method
Commonly used attributes
- Name (thread name)
- StackSize (stackSize)
- By default, the stack size is 512K for both main and child threads (values smaller than 512 do not work).
- The stack size can be set
[NSThread currentThread].stackSize = 1024 * 1024;
- It must be a multiple of 4KB
- isMainTread
- ThreadPriority (threadPriority)
- Priority. It is a floating point number ranging from 0 to 1.0
- 1.0 is the highest,0.0 is the lowest
- The default priority is 0.5
- A high priority only ensures that the CPU scheduling possibility is high, not that the process with a high priority is called first
- QualityOfService (available with iOS8.0)
- NSQualityOfServiceUserInteractive – user interaction, such as drawing or handling user events
- NSQualityOfServiceUserInitiated – user needs
- NSQualityOfServiceUtility – utilities, users do not need to get results immediately
- NSQualityOfServiceBackground – the background
- NSQualityOfServiceDefault – by default, between user needs and utility