A brief analysis on thread deadlock

Source: Internet
Author: User

Scenario 1:

Dispatch_sync (Dispatch_get_main_queue (), ^{        NSLog (@ "does not execute");    });

If you add this code on the main thread, the block that is added to the home row is executed synchronously. This function waits for block execution to return to the main thread, and then executes the following code, and the block is executed until the main thread returns, so the loops wait for the deadlock.

If you change to asynchronous, because the current main thread of a runloop will immediately return, the next time Runloop will execute the host joins block.

Dispatch_async (Dispatch_get_main_queue (), ^{        NSLog (@ "will execute");    });


There is also a method for updating the UI on the main thread, which, if executed on the main thread, does not cause a deadlock even if it is synchronized.

[Self Performselectoronmainthread: @selector (logtest) Withobject:nil Waituntildone:yes];

Scenario 2:

About the Operationqueue.

Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "111");    }];    Nsblockoperation *OP2 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "222");    }];    Nsblockoperation *OP3 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "333");    }];    Nsblockoperation *OP4 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "444");    }];    Nsarray *arr = [Nsarray arraywithobjects:                    op1,                    op2,                    op3,                    OP4,                    Nil];    [[Nsoperationqueue Mainqueue]addoperations:arr Waituntilfinished:no];
    NSLog (@ "2");

is also executed asynchronously in the main thread, so the above code runs correctly and runs in order, because the home row is a serial queue.

Results:

2015-07-01 14:07:34.064 testproject[1388:318202] 12015-07-01 14:07:34.065 testproject[1388:318202] 22015-07-01 14:07:34.070 testproject[1388:318202] 1112015-07-01 14:07:34.071 testproject[1388:318202] 2222015-07-01 14:07:34.073 TESTPROJECT[1388:318202] 3332015-07-01 14:07:34.073 testproject[1388:318202] 444

If you synchronize execution instead, it will cause a deadlock:

Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "111");    }];    Nsblockoperation *OP2 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "222");    }];    Nsblockoperation *OP3 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "333");    }];    Nsblockoperation *OP4 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "444");    }];    Nsarray *arr = [Nsarray arraywithobjects:                    op1,                    op2,                    op3,                    OP4,                    Nil];    [[Nsoperationqueue Mainqueue]addoperations:arr waituntilfinished:yes];    NSLog (@ "2");

Results:

2015-07-01 14:10:48.577 testproject[1423:335286] 1

The primary queue waits for the current run cycle to end and then runs the block in the queue, while the current run loop waits
Addoperations, so the current run cycle never ends, so the block in the main queue never executes, and the program is stuck.
If you create another queue (not the Home column), use synchronous execution:
<pre name= "code" class= "OBJC" >nslog (@ "1");    Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "111");    }];    Nsblockoperation *OP2 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "222");    }];    Nsblockoperation *OP3 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "333");    }];    Nsblockoperation *OP4 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "444");    }];    Nsarray *arr = [Nsarray arraywithobjects:                    op1,                    op2,                    op3,                    OP4,                    Nil];    Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];//concurrency, indeterminate order    [queue Addoperations:arr waituntilfinished: YES];    NSLog (@ "2");

The results of the operation are as follows:

<pre name= "code" class= "OBJC" >2015-07-01 14:14:44.417 testproject[1480:353564] 12015-07-01 14:14:44.418 TESTPROJECT[1480:353689] 1112015-07-01 14:14:44.418 testproject[1480:353705] 4442015-07-01 14:14:44.418 TestProject[ 1480:353690] 3332015-07-01 14:14:44.419 testproject[1480:353685] 2222015-07-01 14:14:44.419 TestProject[1480:353564] 2

The result is normal. Because the newly created queue can be concurrent, the order of execution of the 1,2,3,4 is not necessarily.
If executed asynchronously, the same is true, the 1,2,3,4block is executed in a variable order, but the function returns immediately.
<pre name= "code" class= "OBJC" >nslog (@ "1");    Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "111");    }];    Nsblockoperation *OP2 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "222");    }];    Nsblockoperation *OP3 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "333");    }];    Nsblockoperation *OP4 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "444");    }];    Nsarray *arr = [Nsarray arraywithobjects:                    op1,                    op2,                    op3,                    OP4,                    Nil];    Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];//concurrency, indeterminate order    [queue Addoperations:arr waituntilfinished: NO];    NSLog (@ "2");

Operation Result:
<pre name= "code" class= "OBJC" >2015-07-01 14:17:53.111 testproject[1513:365712] 12015-07-01 14:17:53.112 testproject[1513:365712] 22015-07-01 14:17:53.112 testproject[1513:365832] 3332015-07-01 14:17:53.112 TestProject[ 1513:365831] 1112015-07-01 14:17:53.112 testproject[1513:365837] 4442015-07-01 14:17:53.113 TestProject[1513:365830] 222

Even if the main thread is blocked, the 1,2,3,4 will execute normally.
Scenario 3:
<pre name= "code" class= "OBJC" >nslog (@ "1");    Dispatch_async (dispatch_get_global_queue (0, 0), ^{        NSLog (@ "=================1");        Dispatch_sync (Dispatch_get_main_queue (), ^{            NSLog (@ "=================2");        });        NSLog (@ "=================3");    });    NSLog (@ "2");    while (1) {            }
The block for synchronous execution of the home column does not execute because the current run loop does not end. Because it is synchronous, the following ====3 are not executed.
Operation Result:
<pre name= "code" class= "OBJC" >2015-07-01 14:21:25.499 testproject[1651:389143] 12015-07-01 14:21:25.499 TESTPROJECT[1651:389143] 22015-07-01 14:21:25.499 testproject[1651:389172] =================1
If it is executed asynchronously on the main thread, ====3 executes.
<pre name= "code" class= "OBJC" >nslog (@ "1");    Dispatch_async (dispatch_get_global_queue (0, 0), ^{        NSLog (@ "=================1");        Dispatch_async (Dispatch_get_main_queue (), ^{            NSLog (@ "=================2");        });        NSLog (@ "=================3");    });    NSLog (@ "2");    while (1) {            }
Results:
<pre name= "code" class= "OBJC" >2015-07-01 14:24:54.855 testproject[1687:402116] 12015-07-01 14:24:54.855 TESTPROJECT[1687:402116] 22015-07-01 14:24:54.855 testproject[1687:402152] =================12015-07-01 14:24:54.855 testproject[1687:402152] =================3

Summarize:
Do not synchronize blocks in the master queue in the main thread.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A brief analysis on thread deadlock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.