Series of articles:
multithreading
Multithreading PThreads and NSthreads
Multithreaded GCD
Multithreaded NSOperation
Multithreading
Pthreads
This is a set of multithreading API common on many operating systems, based on the C language framework
#import <pthread.h>// create a thread and perform the task - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {pthread_t thread; // Create a thread and automatically execute pthread_create(&thread, NULL, start, NULL); } void *start(void *data) { NSLog(@"% @", [NSThread currentThread]);
return NULL;
}
Copy the code
A printout
The 2015-07-27 23:57:21. 689testThread[10616:2644653] <NSThread: 0x7fbb48d33690>{number = 2, name = (null)}
Copy the code
You need to manually handle transitions between thread states and manage the lifecycle. For example, this code creates a thread but doesn’t destroy it.
NSThread
The solution is packaged by Apple and fully object-oriented. So you can manipulate thread objects directly, which is very intuitive and convenient. However, its life cycle still needs to be managed manually, so this scheme is also used occasionally, such as [NSThread currentThread], it can get the currentThread class, you can know the various properties of the currentThread, very convenient for debugging. Here’s a look at some of its uses.
Create and start
- Create the thread class first and then start it
OBJECTIVE-C
// create NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run: object:nil); // When the thread is started, the run method of self is executed in thread [thread start];Copy the code
SWIFT
/ / createlet thread = NSThread(target: self, selector: "run:"// Start thread.start()Copy the code
- Create and automatically start a thread
OBJECTIVE-C
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
Copy the code
SWIFT
NSThread.detachNewThreadSelector("run:", toTarget: self, withObject: nil)
Copy the code
- The thread is created (implicitly) using NSObject and automatically started
OBJECTIVE-C
[self performSelectorInBackground:@selector(run:) withObject:nil];
Copy the code
SWIFT
Apple decided that performSelector was unsafe, so they removed it from Swift. Note: The performSelector: method and related selector-invoking methods are not imported in Swift because they are inherently unsafe.
The advantages and disadvantages of the above two ways of creating threads
- Advantages: Simple and quick
- Cons: You can’t set up threads in more detail
Other methods
// Cancel the thread - (void)cancel; // Start thread - (void)start; Thread state @property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isCancelled) BOOL cancelled; // Set and get the thread name -(void)setName:(NSString *)n; -(NSString *)name; + (NSThread *)currentThread; + (NSThread *)mainThread; // Whether the main thread (class method) + (BOOL)isMainThread; // Whether the main thread (object method) - (BOOL)isMainThread; + (void)sleepForTimeInterval:(NSTimeInterval)time; + (void)sleepForTimeInterval:(NSTimeInterval)time; + (void)sleepUntilDate:(NSDate *)date; // Force to stop thread -> enter dead state + (void)exit; // Note: once the thread has stopped (died), the task cannot be started againCopy the code
More detailed blog:
IOS multithreading: a detailed summary of “pthread, NSThread”
It is used to introduce the use method and implementation of Pthread and NSThread in iOS multithreading.
Part ONE: The use of pThreads and other related methods.
Part TWO: The use of NSthreads, thread-related usage, thread state control methods, communication between threads, thread safety and thread synchronization, and thread state transition related knowledge.