Troubleshooting Deadlocks in multiple threads

Source: Internet
Author: User

-(void) Touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *) event

{

//Home row Async

[Self mainqueueasync];

//Home row synchronization

[Self mainqueuesync];

// resolution of deadlocks

[Self demo];

}

// resolution of deadlocks

-(void) demo

{

NSLog(@ "Begin");

// asynchronous

dispatch_async(dispatch_get_global_queue(0, 0), ^{

//Home column The home column performs the task only when the main thread is idle

dispatch_queue_t mainqueue = dispatch_get_main_queue();

//2. Task

dispatch_block_t Task1 = ^ {

[nsthread sleepfortimeinterval: 1.0];

NSLog(@ "Task1%@", [nsthread currentthread]);

};

dispatch_block_t Task2 = ^ {

NSLog(@ "Task2%@", [nsthread currentthread]);

};

// sync

Dispatch_sync(Mainqueue, Task1);

Dispatch_sync(Mainqueue, task2);

});

NSLog(@ "End");

}

Home row Sync deadlock

-(void) Mainqueuesync

{

NSLog(@ "Begin");

//Home column The home column performs the task only when the main thread is idle

dispatch_queue_t mainqueue = dispatch_get_main_queue();

//2. Task

dispatch_block_t Task1 = ^ {

[nsthread sleepfortimeinterval: 1.0];

NSLog(@ "Task1%@", [nsthread currentthread]);

};

dispatch_block_t Task2 = ^ {

NSLog(@ "Task2%@", [nsthread currentthread]);

};

// sync

Dispatch_sync(Mainqueue, Task1);

Dispatch_sync(Mainqueue, task2);

NSLog(@ "End");

}

The task for the Home row asynchronous home column is performed only in the main thread, in turn

-(void) Mainqueueasync

{

//Home row

dispatch_queue_t mainqueue = dispatch_get_main_queue();

//2. Task

dispatch_block_t Task1 = ^ {

[nsthread sleepfortimeinterval: 1.0];

NSLog(@ "Task1%@", [nsthread currentthread]);

};

dispatch_block_t Task2 = ^ {

NSLog(@ "Task2%@", [nsthread currentthread]);

};

// asynchronous

Dispatch_async(Mainqueue, Task1);

Dispatch_async(Mainqueue, task2);

}

Troubleshooting Deadlocks in multiple threads

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.