ㄷㅣㅆㅣ's Amusement

[iOS/Objective-C] To run threads sequentialy or concurrently 본문

Programming/iOS

[iOS/Objective-C] To run threads sequentialy or concurrently

ㄷㅣㅆㅣ 2016. 2. 4. 20:30

[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



0 Comments
댓글쓰기 폼