ios-multithreading dispatch_semaphore_t dispatch_queue_t dispatch_group_t common usage scenarios

Source: Internet
Author: User

    • dispatch_semaphore_tUsage, dispatch_semaphore_create creating semaphores is actually the creation of the object setting the maximum number of concurrent numbers. Must not be less than 0, less than 0 causes a crash, dispatch_semaphore_wait reduce a semaphore, such as the creation of the object semaphore is 5, wait one time to reduce a semaphore, the signal volume is 4, when the semaphore is 0, the current thread will wait Until the semaphore is >0. dispatch_semaphore_signal Adding a semaphore reduces the number of signals that are paired with the increase.
dispatch_semaphore_t semap = dispatch_semaphore_create(1); // 控制最大并发数1条新线程 此时下面操作是线程安全的dispatch_semaphore_wait(semap, DISPATCH_TIME_FOREVER);// 此时信号量由1 变为 0 ,即使下面有任务也不会再执行, 因为信号量为0// 单个线程操作区域... 线程安全dispatch_semaphore_signal(semap);
    • The first common usage group + queue scenario: one-time, multiple tasks executing simultaneously, automatically opening multiple threads, and finally executing the result after the callback processing has finished executing
    • Disadvantage: Unable to control maximum concurrency, operation of the same resource is not secure
// DISPATCH_QUEUE_CONCURRENT 生成一个并发队列(可以有多条线程)    dispatch_queue_t queue = dispatch_queue_create("sendrequest", DISPATCH_QUEUE_CONCURRENT);    // 创建group    dispatch_group_t group = dispatch_group_create();// 多个耗时任务同时进行 加入group中同时进行,执行完毕监听回调    dispatch_group_async(group, queue, ^{        NSLog(@"1");        sleep(2);        NSLog(@"1+");    });    dispatch_group_async(group, queue, ^{        NSLog(@"2");    });    dispatch_group_async(group, queue, ^{        NSLog(@"3");    });    // group任务全部执行完毕回调    dispatch_group_notify(group, queue, ^{        NSLog(@"done");    });
    • The second scenario semaphore + queue : multiple (100 tasks need to be performed, such as database 100 data, need to upload, each create a task, open 2 threads), control the maximum concurrency (2 threads in the background), group can not control the maximum number of concurrent, operation data line loads lock, Ensure thread safety
    • Faults cannot handle callbacks
    dispatch_semaphore_t semap = dispatch_semaphore_create(2); // 控制最大并发数2条新线程    // DISPATCH_QUEUE_CONCURRENT 生成一个并发队列(可以有多条线程)    dispatch_queue_t queue = dispatch_queue_create("sendrequest", DISPATCH_QUEUE_CONCURRENT);    // 10个任务, 最大并发2,     for (int i = 0; i < 10; i++) {        dispatch_async(queue, ^{                dispatch_semaphore_wait(semap, DISPATCH_TIME_FOREVER);                // 线程操作区域                 dispatch_semaphore_signal(semap);            });    }
    • Third scenario combination usage: dispatch_semaphore_t(设置最大并发数) + dispatch_queue_t(创建并发队列) + dispatch_group_t(监听任务完成回调) can handle task completion callback, can set maximum concurrent number
dispatch_semaphore_t semap = dispatch_semaphore_create(2); // 控制最大并发数2条新线程    // DISPATCH_QUEUE_CONCURRENT 生成一个并发队列(可以有多条线程)    dispatch_queue_t queue = dispatch_queue_create("sendrequest", DISPATCH_QUEUE_CONCURRENT);    dispatch_group_t group = dispatch_group_create();        for (int i = 0; i < 10; i++) {        dispatch_group_async(group, queue, ^{            dispatch_semaphore_wait(semap, DISPATCH_TIME_FOREVER);            // 线程操作区域 最多有两个线程在此做事情            dispatch_semaphore_signal(semap);        });    }     // group任务全部执行完毕回调    dispatch_group_notify(group, queue, ^{        NSLog(@"done");    });

ios-multithreading dispatch_semaphore_t dispatch_queue_t dispatch_group_t common usage scenarios

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.