ㄷㅣㅆㅣ's Amusement
[iOS/Objective-C] To run threads sequentialy or concurrently 본문
[iOS/Objective-C] To run threads sequentialy or concurrently
[iOS/Objective-C]에서 thread를 순차적으로 처리하기.
Thread starts when the other Thread ends [on iOS/Objective-C]
여러가지 방법이 있겠지만.... 세마포어를 사용하면 간단하다.
There are so many way to do. but, you had better to use semaphore.
1. Create semaphore
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
2. Do Thread
dispatch_async(......, ^ {
// Do something
........
dispatch_semaphore_signal(sema);
)};
3. Wait for semaphore signal
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
-------------------------------------------------------------------------------
[iOS/Objective-C]에서 thread들을 병렬로 처리하고 모든 thread가 끝났을때 noti 받기
Process Threads concurrently and then, receive all-threads-done notification.
1. Create dispatch_group
dispatch_group_t taskGroup = dispatch_group_create();
2.Dispatch group async each threads
dispatch_group_async(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[NSThread sleepForTimeInterval:10.0];
});
dispatch_group_async(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[NSThread sleepForTimeInterval:5.0];
});
3. Receive notification
dispatch_group_notify(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// something to do after two tasks...
});
-------------------------------------------------------------------------
Example
- 순차적으로 2가지 Thread를 돌리는 Thread와, 5초동안 수행되는 1 Thread를 병렬로 처리하기.
- To run two threads concurrently. one is running two inner threads sequentially and another is running 5 seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - (IBAction)btnClicked:(id)sender { dispatch_group_t taskGroup = dispatch_group_create(); dispatch_group_async(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { dispatch_semaphore_t sema = dispatch_semaphore_create(0); [[YOUR_TASK_FIRST doWithCompleteBlock:^(BOOL success) { if (success) { [[YOUR_TASK_SECOND doWithCompleteBlock:^(BOOL success) { // DO something... }]; dispatch_semaphore_signal(sema); } }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); [[YOUR_TASK_ANOTHER doWithCompleteBlock:^(BOOL success) { if (success) { // DO something... } }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); }); dispatch_group_async(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { [NSThread sleepForTimeInterval:5.0]; }); dispatch_group_notify(taskGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { // something to do after two tasks... }); } | cs |
'Programming > iOS' 카테고리의 다른 글
[iOS/Objective-C] Video Player tutorial using AVPlayer (AVFoundation) (2) | 2016.02.23 |
---|---|
[iOS/Objective-C] 이미지 자르기 (Image crop), CGContextSetBlendMode usage (0) | 2016.02.05 |
[iOS/Objective-C] AES/CBC/NoPadding using CCCRypt (0) | 2015.12.16 |
[iOS/Objective-c] UIButton background color 설정하기. (6) | 2015.11.30 |
[iOS/Objective-c] Touch Id 사용하여 인증하기. (passcode도 동작) (0) | 2015.11.25 |