cool hit counter What you need to know about ios multithreading_Intefrankly

What you need to know about ios multithreading

Before understanding multithreading it is important to first understand a few concepts


A process is an ongoing application in the system; each process is independent of each other, and each process runs in its own dedicated and protected memory space.


For a process to perform a task, it must have threads (at least one thread per process), the smallest unit in the process that performs operations, an entity in the process, the basic unit that is independently scheduled and dispatched by the system ;


Multiple threads can be opened in a process, each of which can perform different tasks in parallel (simultaneously). Multi-threading techniques can improve the efficiency of program execution.

Multi-threading principle

A single core CPU can handle 1 thread at the same time, and multiple threads executing concurrently is actually the CPU quickly scheduling between multiple threads. If the CPU dispatches threads fast enough, it creates the illusion of multiple threads executing concurrently.

Advantages and disadvantages of multithreading

Advantages: can properly improve the efficiency of program execution; can properly improve resource utilization (CPU, memory utilization). Disadvantages: opening threads requires a certain amount of memory space (by default, the main thread occupies 1M and the child threads occupy 512KB), if you open a large number of threads, it will occupy a lot of memory space and reduce the performance of the program; the more threads, the greater the CPU overhead in scheduling threads; the program design is more complicated, such as communication between threads, data sharing of multiple threads.

The difference between synchronous and asynchronous

Synchronous: can only perform tasks in the current thread, does not have the ability to open new threads Asynchronous: can perform tasks in new threads, with the ability to open new threads

Concurrent and Serial

Concurrent: multiple tasks are executed concurrently (simultaneously) Serial: one task is executed and then the next one is executed

How to communicate between threads


[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];

- (void)updateUI {
    // UI Update code
    self.alert.text = @"Thanks!";


[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // UI Update code
    self.alert.text = @"Thanks!";


dispatch_async(dispatch_get_main_queue(), ^{
   // UI Update code
   self.alert.text = @"Thanks!";
The use of multithreading in ios


Use of NSThread

NSThread creates threads /** Method 1, requires START */

NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething1:) object:@"NSThread1"];
 // Threads are added to the thread pool to wait for CPU scheduling, the time is fast and execution is almost immediate
[thread1 start];

- (void)doSomething1:(NSObject *)object {
     // Parameters passed in
    NSLog(@"doSomething1:%@",[NSThread currentThread]);

/** Method 2, created and started automatically */

[NSThread detachNewThreadSelector:@selector(doSomething2:) toTarget:self withObject:@"NSThread2"];

- (void)doSomething2:(NSObject *)object {
    NSLog(@"doSomething2:%@",[NSThread currentThread]);

/** Method 3, implicit creation, direct start */

[self performSelectorInBackground:@selector(doSomething3:) withObject:@"NSThread3"];

- (void)doSomething3:(NSObject *)object {
    NSLog(@"doSomething3:%@",[NSThread currentThread]);

Return to the current thread

 // Current thread
[NSThread currentThread];
NSLog(@"%@",[NSThread currentThread]);

blocking dormancy

 // How long does it stay dormant
[NSThread sleepForTimeInterval:2];
 //Sleep to specified time
[NSThread sleepUntilDate:[NSDate date]];

Some other methods

 //Exit the thread
[NSThread exit];
 // determine if the current thread is the main thread
[NSThread isMainThread];
 // determine if the current thread is multi-threaded
[NSThread isMultiThreaded];
 // Object of the main thread
NSThread *mainThread = [NSThread mainThread];
GCD understanding and use

There are 2 core concepts in GCD (1) Task: What operation to perform (2) Queue: used to store tasks


Whatever the queue is, it must be a FIFO queue, i.e., first in, first out. So, please remember: whether it's a SerialQueue or a ConcurrencyQueue, it's a FIFO queue. It also means that tasks must be executed one by one, in a first-in-first-out order.

Serial queues. When creating a queue, pass the parameter DISPATCH_QUEUE_SERIAL to indicate the creation of a serial queue. Tasks will be executed one by one, and only when the previous task is completed will the next task be continued. Serial execution does not mean synchronous execution, so be sure to make the distinction.

Concurrent queues. When creating a queue, pass the parameter DISPATCH_QUEUE_CONCURRENT to indicate the creation of a concurrent queue. The concurrent queue will create as many threads as possible to execute the task. The tasks in the concurrent queue will execute the tasks in the order they are entered, but it is uncertain which task will complete first.

serial queue

dispatch_queue_t serialQueue = dispatch_queue_create("serial_queue", 
dispatch_async(serialQueue, ^{
dispatch_async(serialQueue, ^{
dispatch_async(serialQueue, ^{

 printable s1,s2,s3

concurrent queue

dispatch_queue_t concurrencyQueue = dispatch_queue_create("com.huangyibiao.concurrency-queue",
    dispatch_async(concurrencyQueue, ^{
    dispatch_async(concurrencyQueue, ^{
    dispatch_async(concurrencyQueue, ^{

 printable s1,s3,s2

Synchronous asynchronous threads

 // Global concurrent queue acquisition method
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

 // Synchronization of task creation methods
dispatch_sync(queue, ^{
     // Put the synchronous execution task code here
 // Asynchronous execution of task creation methods
dispatch_async(queue, ^{
     // Put the asynchronous execution task code here

1、Can we have a good New Years Eve and change the bank on February 16
2、Jufeng helps Chongqing 10000 tons of cold storage
3、C Programming Habits Insights
4、Hightech police robot developed in China now in Dubai Another career will be in crisis
5、China to Remake AIThemed Drama The Real Humans Starring Ma Tianyu Qi Wei

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送