Multithreading--C language

Source: Internet
Author: User

rely on C language , to execute , the most efficient

I. Serial queue

//1. Create a channeling queue

//(1) get the created serial queue to implement thread synchronization in the main Thread

dispatch_queue_t quene1 = dispatch_get_main_queue();

//(2) create a serial queue yourself , tasks are implemented in child threads

dispatch_queue_t quene2 = dispatch_queue_create("Com.lanou3g.oa", Dispatch_queue_ SERIAL); // string -- function of the unique identifier ( anti-domain form ) dispatch_queue_serial-- specified as serial queue

//2. Add a task to the network queue

Dispatch_async(quene2, ^{

NSLog(@ " task 1:%@", [nsthread currentthread]);

});

...

// release

dispatch_release(quene2);

Two. Parallel queue

//1. Create a parallel queue

//(1) get the created parallel queue

dispatch_queue_t quene1 = dispatch_get_global_queue(dispatch_queue_priority_default, 0); // two parameters are priority , reserved parameters

//(2) Create your own concurrency

dispatch_queue_t quene2 = dispatch_queue_create("Com.lanou3g.oa", Dispatch_queue_ CONCURRENT); // parameter 1-- unique indicator

// parameter 2-- specified as concurrent queue

//2 Adding tasks to the queue

Dispatch_async(quene2, ^{

NSLog(@ " task 1:%@", [nsthread currentthread]);

});

...

// request picture

// jump to main thread refresh interface

dispatch_async(dispatch_get_main_queue(), ^{

// update UI display picture

});

// release

dispatch_release(quene2);

Three. Grouping Tasks

//1 Creating a parallel queue

dispatch_queue_t quene = dispatch_get_global_queue(dispatch_queue_priority_default, 0 );

//2 creating groups

dispatch_group_t group = dispatch_group_create();

//3 Just add a task to a group

Dispatch_group_async(Group, Quene, ^{

NSLog(@ " task one request 0-20m data ");

});

...

//4 When all tasks within a group are completed , the stitching operation

dispatch_group_notify(Group, Quene, ^{

NSLog(@ " stitching data !") );

});

// release

dispatch_release(group);

Four. Single-case variables

Static Singleton *single = nil;

Static dispatch_once_t oncetoken;

dispatch_once(&oncetoken, ^{

single = [[Singleton alloc]init];

});

Five. Obstacles

The role of the//obstacle task : You can guarantee that the concurrency task after the barrier must be completed before the execution of the task is executed before the obstacle can begin execution

// Note : to add an obstacle task , you must use the concurrency task you created

// Create concurrent Tasks

dispatch_queue_t queue = dispatch_queue_create("Com.lanoug.oa", Dispatch_queue_ CONCURRENT);

// add tasks to the queue

dispatch_async(queue, ^{

NSLog(@ "A Write file ");

});

...

// add ( set ) Barrier task

dispatch_barrier_async(queue, ^{

NSLog(@ " here is the Roadblock mission ");

});

dispatch_async(queue, ^{

NSLog(@ "E Read file ");

});

// release

dispatch_release(queue);

Six. Delay

dispatch_after(dispatch_time(dispatch_time_now, (int64_t) (5 * nsec_ PER_SEC)), dispatch_get_main_queue(), ^{

NSLog(@ "5 seconds True man ");

});

Dispatch_get_main_queue is executed in the main thread ( default ), if you want to execute in a child thread , This is a child thread here ;

Seven. repeated execution

dispatch_apply( dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^ (size_t index) {

NSLog(@ " haha -%ld", index);

}); ///size_t to add parameter names such as : Index

***********************

dispatch_apply( dispatch_get_main_queue(), ^ (size_t index) {

NSLog(@ " haha -%ld", index);

});

Multithreading--C language

Related Article

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.