- dispatch semaphore
1.dispatch semaphore是持有計數的信號,該計數是多線程編程中的計數類型信號。計數為0時等待,計數為1或大于1時,減去1而不等待。
2.dispatch_semaphore_wait函數等待dispatch semaphore的計數值大于或等于1,當計數值大于等于1,或者等待中計數值大于等于1時,對該計數進行減法并從dispatch_semaphore_wait函數返回。
3.dispatch_semaphore_wait函數的返回值也與dispat
ch_group_wait函數相同。
dispatch_time_t time;//定義dispatch_time_t;
long result = dispatch_semaphore_wait(semaphore,time);
if(result == 0){
//在time定義的待機等待時間中如果計數值大于或者等于1,則返回0,可執行需要進行排他控制的任務。任務結束通過dispatch_semaphore_signal函數將dispatch semaphore的計數值加1.
}else{
//計數值為0,因此在等待指定時間后返回非0;
}
- dispatch_once
diapatch_once函數是保證在應用程序執行中只執行一次指定的api。
static dispatch_once_t token;
dispatch_once(&token,^{
});
這個函數比用控制變量控制更可靠。