GCD使用dispatch_semaphore_t創建多線程網絡同步請求


一、簡介:

dispatch_semaphore_t:表示信號,生成信號的方法是

dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);

其中參數0表示該新生成信號的總的信號量為0個。

dispatch_semaphore_wait:信號等待,它像一個安保,比如小區規定最多只能進入3輛車,而進入一輛車后名額就會減少一個,當剩下的名額為0的時候,再有汽車說要進去時,就只能在外面等待了,直到有名額閑置出來了,才能開進小區。

dispatch_semaphore_signal:信號釋放,當有一輛車從小區出來時,就騰出來了一個名額。

 二、驗證

這三個常用方法解釋就暫且到這里,下面用代碼來驗證下上面介紹的作用。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self syncAction1];
}

- (void)syncAction1{
    
    //使用GCD的信號量 dispatch_semaphore_t 創建同步請求
    dispatch_group_t group =dispatch_group_create();
    dispatch_queue_t globalQueue=dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, globalQueue, ^{
        dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
        //模擬網絡多線程耗時操作
        dispatch_group_async(group, globalQueue, ^{
            sleep(3);
            NSLog(@"%@---block1結束。。。",[NSThread currentThread]);
            dispatch_semaphore_signal(semaphore);
        });
        NSLog(@"%@---1結束。。。",[NSThread currentThread]);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });
    dispatch_group_async(group, globalQueue, ^{
        dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
        
        //模擬網絡多線程耗時操作
        dispatch_group_async(group, globalQueue, ^{
            sleep(3);
            NSLog(@"%@---block2結束。。。",[NSThread currentThread]);
            dispatch_semaphore_signal(semaphore);
        });
        
        NSLog(@"%@---2結束。。。",[NSThread currentThread]);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });

    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"%@---全部結束。。。",[NSThread currentThread]);
        
    });
    
}

運行APP,點擊頁面,打印的結果如下:

2016-12-22 23:45:36.633 GCDTest[852:27326] <NSThread: 0x7fce485154f0>{number = 2, name = (null)}---1結束。。。
2016-12-22 23:45:36.633 GCDTest[852:27328] <NSThread: 0x7fce48606f40>{number = 3, name = (null)}---2結束。。。
2016-12-22 23:45:39.633 GCDTest[852:27329] <NSThread: 0x7fce48512ea0>{number = 4, name = (null)}---block1結束。。。
2016-12-22 23:45:39.633 GCDTest[852:27330] <NSThread: 0x7fce484a9960>{number = 5, name = (null)}---block2結束。。。
2016-12-22 23:45:39.634 GCDTest[852:27328] <NSThread: 0x7fce48606f40>{number = 3, name = (null)}---全部結束。。。

結論:通過打印結果可以驗證,

1.只有當剩余信號總量大於0時線程才能通過dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);方法,繼續向下執行。

2.當dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);下面的代碼無法執行處於等待情況下時,dispatch_semaphore_signal(semaphore);一旦執行,釋放出自己的信號,則代碼又可以愉快的往下執行了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM