OBJC's GCD under the Apple platform, multithreaded programming is elegant and natural.

Source: Internet
Author: User
Tags gcd

In Apple's operating system platform, GCD makes multithreaded programming so elegant and natural.
In the traditional multi-threaded programming, the first is to write the thread processing loop, and then the event queue, message queue, as well as the thread loop to separate the event interpretation messages, dispatch processing, but also to consider whether to synchronize between threads, but also write a lot of potentially confusing function name callback handler, register callback program, And the code is scattered even if the same file is not easy to see which threads correspond to each other or whether there is a sequential or concurrent relationship, it is not conducive to debugging, but also to consider whether to use the thread pool, the threading line to use what mode, and so on.
In the OBJC of the Apple platform, only the following are required:

*^{    //  handle A in step 1;    Dispatch_async (queue_concurrent, ^{        //  handle A in step 2;        Dispatch_async (Queue_main, ^{            //  handle A in step 3;         });    });});

Elegance, at least that's what I thought. At a glance, the entire process is divided into three sub-processes, each running in a different thread. The sub-process can be concurrent, the second sub-process can be parallel, each sub-process in which thread execution (, in addition to Queue_main and Queue_backgroup, the rest are worker threads), is written to think.
The shadow of the traditional multi-threaded programming is no trace, only know I wrote here to use the multi-path line asynchronous execution of my task, ok I say a sound asynchronous execution.

It is not that multithreaded programming is only the same, but in the traditional multithreaded programming, the implementation of the above code, but also a lot of effort. It is not easy to understand that there are so many pseudo-codes written without any hesitation.

structthread{voidPUTQ (msg); UINTCreateThread ();};structTHREADSTEP1: Publicthread{Queue q; voidregister (handler); voidRun () {wait (q); Msg=GETQ (q); Handler=Findhandler (msg);    Handler (msg); } };structTHREADSTEP2: Publicthread{Staticqueue q; Static voidregister (handler); voidRun () {wait (threadstep2::q); Msg=getq (THREADSTEP2::Q); Handler=Findhandler (msg);    Handler (msg); } };structthreadstep2pool{ThreadStep2 Threads[n]; voidcreatethreads (); voidregister (handler);};structTHREADSTEP3: Publicthread{Queue q; voidregister (handler); voidRun () {wait (q); Msg=GETQ (q); Handler=Findhandler (msg);    Handler (msg); } };voidHandler1 (void*CTX)
{
Handle Get_a (CTX) in step 1
GET_P2 (CTX)->putq (CTX);
}
voidHandler2 (void*CTX)
{
Handle Get_a (CTX) in step 2
GET_T3 (CTX)->putq (CTX);
}voidHandler3 (void*CTX)
{
Handle Get_a (CTX) in Step 3
}
Main () {    ThreadStep1 t1;    Threadstep2pool P2;    THREADSTEP3 T3;        T1.createthread ();    P2.createthreads ();    T1.register (handler1);    P2.register (handler2);    T3.register (HANDLER3);        Anew  A;    Msg msg (A, T1, p2, T3);    T1.PUTQ (msg);    T3.run ();}

OBJC's GCD under the Apple platform, multithreaded programming is elegant and natural.

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.