SlideShare a Scribd company logo
1 of 17
Grand Central Dispatch
S.O.Lab develop By oracleOn
흑마술
회전하는 마우스 커서 와 처리중 메세지
‘ ’웹 기반 프로그래밍 분야에서 새로고침
백그라운드 프로세싱백그라운드 프로세싱
스레드스레드
iOS Muti Tasking
멀티코어 프로세서의 등장
다중 Thread 프로그래밍 구현욕구
iPhone 3GS : S5PC100 600MHz : 싱글코어 / iPhone 4 : A4(4): 싱글 코어
iPhone 4s : A5 : 듀얼코어 / iPhone 5 : A6: : 듀얼코어
Apple 의 계획 ? 의도 ?
WWDC2009 처음 GCD 가 공개되는 순간
http://www.youtube.com/watch?v=nhbA9jZyYO4
GCC ==> LLVM ==> LLVM 2.0
LLVM 덕분에 가능해진 기술이 Block 과 GCD
메모리 관리와 Block 코딩
iOS 4.0 부터 멀티태스킹을 지원
메모리 부족문제를 GCD 를 이용하여 개선하고자 하였음
GCD 를 구현하기위한 Block 코딩 (GCD 는 Block-based API)
Block : 코드자체 ( 데이터 , 함수 , 클래스 ) 를 객체화 할수 있는것
ios Thread
NSThread 실험 1 (addSubview 와 Thread)
- (void)viewDidLoad
{
[super viewDidLoad];
_countlabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
_countlabel.text = @"0";
[self.view addSubview:_countlabel];
_goBt =[UIButton buttonWithType:UIButtonTypeCustom];
_goBt.frame = CGRectMake(100, 200, 100, 30);
[_goBt setTitle:@"go Second page" forState:UIControlStateNormal];
[_goBt addTarget:self action:@selector(goSecondPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_goBt];
[NSThread detachNewThreadSelector:@selector(firstThread) toTarget:self withObject:nil];
}
-(void) firstThread
{
for (int i=0; i<100; i++){
NSString *t = [NSString stringWithFormat:@"%i",[_countlabel.text intValue]+1];
[self performSelectorOnMainThread:@selector(mainThreadSetText:) withObject:t waitUntilDone:YES];
[NSThread sleepForTimeInterval:1.0];
}
}
-(void)mainThreadSetText:(NSString *)text
{
_countlabel.text = text;
NSLog(@"count: %@",_countlabel.text);
}
-(void) goSecondPage
{
secondView *tempView = [[secondView alloc]init];
[self.view addSubview:tempView.view];
}
ios Thread
NSThread 실험 1 (addSubview 와 Thread)
- (void)viewDidLoad
{
[super viewDidLoad];
_countlabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
_countlabel.text = @"0";
[self.view addSubview:_countlabel];
_goBt =[UIButton buttonWithType:UIButtonTypeCustom];
_goBt.frame = CGRectMake(100, 200, 100, 30);
[_goBt setTitle:@"go Second page" forState:UIControlStateNormal];
[_goBt addTarget:self action:@selector(goSecondPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_goBt];
[NSThread detachNewThreadSelector:@selector(firstThread) toTarget:self withObject:nil];
}
-(void) firstThread
{
for (int i=0; i<100; i++){
NSString *t = [NSString stringWithFormat:@"%i",[_countlabel.text intValue]+1];
[self performSelectorOnMainThread:@selector(mainThreadSetText:) withObject:t waitUntilDone:YES];
[NSThread sleepForTimeInterval:1.0];
}
}
-(void)mainThreadSetText:(NSString *)text
{
_countlabel.text = text;
NSLog(@"count: %@",_countlabel.text);
}
-(void) goSecondPage
{
secondView *tempView = [[secondView alloc]init];
[self.view addSubview:tempView.view];
}
ios Thread
NSOperation
* 전역변수에 두개이상의 스래드에서 동시에 접근하는 경우
* 많은 수의 스래드 생성이 발생하였을때 리소스와 퍼포먼스의 저하
1. 직접 큐를 만들어서 NSThread 갯수를 제어하는 방식
- NSMutableArray 로 큐용 배열를 만들어두고 스래드 호출하면 큐에 넣는다 .
- 현재 돌고있는 스래드 수를 확인한후 설정한 최대의 스래드 수를 넘지 않으면 스래
드를 만들어서 진행한다 .
- 전부 사용중일때는 하나가 끝날때 까지 기다린다 .
NSOperation / NSOperationQueueNSOperation / NSOperationQueue
구현하기도 복잡하고 , 상황에 대처하여 제어하기 어렵다 .
NSOperationQueue
LIFO : 스택 / FIFO : 큐
NSOperationQueue 는 NSOperation 을 담는 큐이며 FIFO 방식으로 들어간 순서대로 NSOperation 을 실행 시켜
주는 기능
NSOperation 은 NSThread 를 만들때 직접 함수와 그 함수가 들어있는 객체 (target) 를 지정해주는것과 대조
NSOperation 은 실행 함수도 직접 자기자신으로 지정해두는것이 NSThread 와 다른점
#import <Foundation/Foundation.h>
@interface TempOperation : NSOperation
@property(assign, nonatomic) int countNum;
@end
#import "TempOperation.h"
@implementation TempOperation
-(void) main
{
NSLog(@"Start : %i",_countNum);
[NSThread sleepForTimeInterval:1.0];
NSLog(@"End: %i",_countNum);
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
int i =0;
[queue setMaxConcurrentOperationCount:2]; // 최대 큐수
TempOperation *test = [[TempOperation alloc]init];
test.countNum = i++;
[queue addOperation:test];
test = [[TempOperation alloc]init];
test.countNum = i++;
[queue addOperation:test];
test = [[TempOperation alloc]init];
test.countNum = i++;
[queue addOperation:test];
test = [[TempOperation alloc]init];
test.countNum = i++;
[queue addOperation:test];
NSLog(@"queue adding complete");
}
NSInvocationOperation Class
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Load" style:UIBarButtonItemStyleDone target:self
action:@selector(loadData)];
NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000];
self.array = _array;
[_array release];
}
- (void) loadData {
/* Operation Queue init (autorelease) */
NSOperationQueue *queue = [NSOperationQueue new];
/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(loadDataWithOperation)
object:nil];
/* Add the operation to the queue */
[queue addOperation:operation];
[operation release];
}
- (void) loadDataWithOperation {
NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"];
NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL];
for(NSString *str in tmp_array) {
[self.array addObject:str];
}
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}
The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a
single encapsulated task specified as an invocation
You can use this class to initiate an operation that consists of invoking a selector on a specified object
Grand Central Dispatch
1. NSOperationQueue 가 있듯이 GCD 에는 dispatch_queue_t
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
dispatch_queue_t dQueue = dispatch_queue_create(“com.apple.testQueue”, NULL);
dispatch_queue_create
dispatch_async(dQueue, ^{
...
}); // dispatch_async 는 큐에 블럭을 넣는 일을 하는
함수
dispatch_get_main_queue();
GCD 의 큐의 경우 무조건 한번에 하나만 실행 ~~!!!
Grand Central Dispatch
메인큐는 메인스레드상에서
dispatch_get_main_queue()
직접 만든 큐는 하나의 스레드를 할당 받고 사용
dispatch_queue_create
글로벌 큐는 큐에 넣는대로 바로 스레드를 만들어서 실행
dispatch_get_global_queue();
#import "ViewController.h"
#include <dispatch/dispatch.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dispatch_queue_t dqueue = dispatch_queue_create("test01", NULL);
__block int i =0;
dispatch_async(dqueue, ^{
NSLog(@"GCD : %i",i);
[NSThread sleepForTimeInterval:2.0];
NSLog(@"GCD %i END",i);
i++;
});
dispatch_async(dqueue, ^{
NSLog(@"GCD : %i",i);
[NSThread sleepForTimeInterval:2.0];
NSLog(@"GCD %i END",i);
i++;
});
}
[queue setMaxConcurrentOperationCount:1];
dispatch_queue_create // 최대큐를 1 개로 지정한것과 같다 .
[queue setMaxConcurrentOperationCount:100];
dispatch_get_global_queue(); // 최대큐를 무한대까지 지정한 것과
같다 .
Semaphore & Mutex
Semaphore : 공유 리소스에 접근할 수 있는 최대 허용치만큼 동시에 사용자 접근을 할 수 있게하는 것
Mutex : 한 번에 하나의 쓰레드만이 실행되도록 하는 재 입장할 수 있는 코드 섹션에 직렬화된 접근을 허용하
는것
프로세스 간 메시지를 전송하거나 , 혹은 공유메모리를 통해서 특정 data 를 공유하게 될 경우 발생하는
문제는 , 공유된 자원에 여러 개의 프로세스가 동시에 접근 하면서 발생한다 . 단지 , 한번에 하나의 프
로세스만 접근 가능하도록 만들어 줘야 하고 , 이때 Semaphore 를 쓴다 .
뮤텍스는 값이 1 인 세마포어
리소스
Thread
Semaphore & Mutex
Dispatch_semaphore_create(3)
리소스
Thread
뮤텍스와 세마포어 .. 언떤 상황에서 어떻게 사용하는 것이 좋
은가 ?
Grand Central Dispatch
dispatch_semaphore_t Tsemaphore = dispatch_semaphore_create(3);
dispatch_semaphore_wait ==> 먼저 하나의 블럭이 실행되면 wait 을 호출 ==> 내부 카운트를 1 올린다 .
그리고 내부 카운트가 먼저 생성할때 설정한 3 보다 적으면 바로 돌아가고 , 많을 경우 Sleep 을 걸어버린
다 .
dispatch_semaphore_signal ==> 내부 카운트 1 내린다 .
그리고 다른 블럭에서 작업이 끝날때 signal 을 날려주면 내부 카운트가 하나 줄게된다 .
내부 카운트가 줄었을때 설정한 3 보다 적으면 재우던 스레드를 깨워서 진행 시킨다 .
dispatch_semaphore_t Tsignal = dispatch_semaphore_create(2); //2 개 짜리 세마포어 생성
dispatch_async(dqueue, ^{
dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"GCD : %i 스레드 시작 ",i);
[NSThread sleepForTimeInterval:2.0];
NSLog(@"GCD : %i 스레드 끝 ",i);
dispatch_semaphore_signal(Tsignal);
});
});
Grand Central Dispatch
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dispatch_queue_t dqueue = dispatch_queue_create("test01", NULL);
//__block int i =0;
int i =0;
dispatch_semaphore_t Tsignal = dispatch_semaphore_create(2); //2 개 짜리 세마포어 생성
dispatch_async(dqueue, ^{
dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"GCD : %i 스레드 시작 ",i);
[NSThread sleepForTimeInterval:2.0];
NSLog(@"GCD : %i 스레드 끝 ",i);
dispatch_semaphore_signal(Tsignal);
});
});
i++;
dispatch_async(dqueue, ^{
dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"GCD : %i 스레드 시작 ",i);
[NSThread sleepForTimeInterval:2.0];
NSLog(@"GCD : %i 스레드 끝 ",i);
dispatch_semaphore_signal(Tsignal);
});
});
i++;
...........
}
감사합니다 .

More Related Content

What's hot

[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요
[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요
[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요NAVER D2
 
Free rtos seminar
Free rtos seminarFree rtos seminar
Free rtos seminarCho Daniel
 
스톰 미리보기
스톰 미리보기스톰 미리보기
스톰 미리보기June Yi
 
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기흥배 최
 
Multi-thread : producer - consumer
Multi-thread : producer - consumerMulti-thread : producer - consumer
Multi-thread : producer - consumerChang Yoon Oh
 
[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스종빈 오
 
Windws via c/c++ chapter 6
Windws via c/c++ chapter 6Windws via c/c++ chapter 6
Windws via c/c++ chapter 6SukYun Yoon
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSCirculus
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttpNAVER D2
 
Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본ssuser0c2478
 
11 윈도우스레드풀
11 윈도우스레드풀11 윈도우스레드풀
11 윈도우스레드풀ssuser0c2478
 
Profiling - 실시간 대화식 프로파일러
Profiling - 실시간 대화식 프로파일러Profiling - 실시간 대화식 프로파일러
Profiling - 실시간 대화식 프로파일러Heungsub Lee
 
07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도ssuser3fb17c
 
헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리은숙 이
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript Dahye Kim
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?NAVER D2
 
Memory & object pooling
Memory & object poolingMemory & object pooling
Memory & object poolingNam Hyeonuk
 
11_웹서비스활용
11_웹서비스활용11_웹서비스활용
11_웹서비스활용noerror
 

What's hot (20)

[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요
[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요
[2B7]시즌2 멀티쓰레드프로그래밍이 왜 이리 힘드나요
 
Lock free queue
Lock free queueLock free queue
Lock free queue
 
Free rtos seminar
Free rtos seminarFree rtos seminar
Free rtos seminar
 
스톰 미리보기
스톰 미리보기스톰 미리보기
스톰 미리보기
 
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
 
Multi-thread : producer - consumer
Multi-thread : producer - consumerMulti-thread : producer - consumer
Multi-thread : producer - consumer
 
[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스
 
Windws via c/c++ chapter 6
Windws via c/c++ chapter 6Windws via c/c++ chapter 6
Windws via c/c++ chapter 6
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
 
Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본
 
Nodejs_chapter3
Nodejs_chapter3Nodejs_chapter3
Nodejs_chapter3
 
11 윈도우스레드풀
11 윈도우스레드풀11 윈도우스레드풀
11 윈도우스레드풀
 
Profiling - 실시간 대화식 프로파일러
Profiling - 실시간 대화식 프로파일러Profiling - 실시간 대화식 프로파일러
Profiling - 실시간 대화식 프로파일러
 
07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도
 
헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?
 
Memory & object pooling
Memory & object poolingMemory & object pooling
Memory & object pooling
 
11_웹서비스활용
11_웹서비스활용11_웹서비스활용
11_웹서비스활용
 

Viewers also liked

Every soul a star (1)
Every soul a star (1)Every soul a star (1)
Every soul a star (1)Natasia Hyde
 
COMPORTAMIENTOS DIGITALES
COMPORTAMIENTOS DIGITALES COMPORTAMIENTOS DIGITALES
COMPORTAMIENTOS DIGITALES lindaduitama
 
Legislative Update: New Legislation and Political Developments Affecting Empl...
Legislative Update: New Legislation and Political Developments Affecting Empl...Legislative Update: New Legislation and Political Developments Affecting Empl...
Legislative Update: New Legislation and Political Developments Affecting Empl...Parsons Behle & Latimer
 
Objective-C에서 멀티스레드 사용하기
Objective-C에서 멀티스레드 사용하기Objective-C에서 멀티스레드 사용하기
Objective-C에서 멀티스레드 사용하기Jaeeun Lee
 
Davinder Lahil - Land Development
Davinder Lahil - Land DevelopmentDavinder Lahil - Land Development
Davinder Lahil - Land DevelopmentDavinder Lahil
 
Employee Corrective Action
Employee Corrective ActionEmployee Corrective Action
Employee Corrective ActionBethany Platt
 
Trabajo alis
Trabajo alisTrabajo alis
Trabajo alisalisro
 
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...WineAid - Wir helfen Kindern
 
Presentation du voyage aux Vans 2012
Presentation du voyage aux Vans 2012Presentation du voyage aux Vans 2012
Presentation du voyage aux Vans 2012Jeff Simon
 
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssss
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssssDiapositivaspaulofreire 091103175943-phpapp02.pptxsssss
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssss8328alrg
 
Ventisquero Clasico Sauvignon Blanc
Ventisquero Clasico Sauvignon BlancVentisquero Clasico Sauvignon Blanc
Ventisquero Clasico Sauvignon BlancKarim
 

Viewers also liked (20)

Prosa xiii xiv
Prosa xiii xivProsa xiii xiv
Prosa xiii xiv
 
Book1
Book1Book1
Book1
 
Every soul a star (1)
Every soul a star (1)Every soul a star (1)
Every soul a star (1)
 
Informe DERECHOS HUMANOS
Informe DERECHOS HUMANOSInforme DERECHOS HUMANOS
Informe DERECHOS HUMANOS
 
COMPORTAMIENTOS DIGITALES
COMPORTAMIENTOS DIGITALES COMPORTAMIENTOS DIGITALES
COMPORTAMIENTOS DIGITALES
 
Legislative Update: New Legislation and Political Developments Affecting Empl...
Legislative Update: New Legislation and Political Developments Affecting Empl...Legislative Update: New Legislation and Political Developments Affecting Empl...
Legislative Update: New Legislation and Political Developments Affecting Empl...
 
Objective-C에서 멀티스레드 사용하기
Objective-C에서 멀티스레드 사용하기Objective-C에서 멀티스레드 사용하기
Objective-C에서 멀티스레드 사용하기
 
Davinder Lahil - Land Development
Davinder Lahil - Land DevelopmentDavinder Lahil - Land Development
Davinder Lahil - Land Development
 
Carta de presentacion
Carta de presentacionCarta de presentacion
Carta de presentacion
 
1 numeros enteroyass
1 numeros enteroyass1 numeros enteroyass
1 numeros enteroyass
 
Jsoft_Cert
Jsoft_CertJsoft_Cert
Jsoft_Cert
 
Employee Corrective Action
Employee Corrective ActionEmployee Corrective Action
Employee Corrective Action
 
Trabajo alis
Trabajo alisTrabajo alis
Trabajo alis
 
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...
WineAid - das Fundraising mit Mehrwert und Geschmack. Genuss der Hilft - egal...
 
Test
TestTest
Test
 
Presentation du voyage aux Vans 2012
Presentation du voyage aux Vans 2012Presentation du voyage aux Vans 2012
Presentation du voyage aux Vans 2012
 
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssss
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssssDiapositivaspaulofreire 091103175943-phpapp02.pptxsssss
Diapositivaspaulofreire 091103175943-phpapp02.pptxsssss
 
Sept merveilles
Sept merveillesSept merveilles
Sept merveilles
 
Herzattacke
HerzattackeHerzattacke
Herzattacke
 
Ventisquero Clasico Sauvignon Blanc
Ventisquero Clasico Sauvignon BlancVentisquero Clasico Sauvignon Blanc
Ventisquero Clasico Sauvignon Blanc
 

Similar to Gcd ppt

C# Game Server
C# Game ServerC# Game Server
C# Game Serverlactrious
 
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?내훈 정
 
[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성NAVER D2
 
Introduction to Fork Join Framework_SYS4U I&C
Introduction to Fork Join Framework_SYS4U I&CIntroduction to Fork Join Framework_SYS4U I&C
Introduction to Fork Join Framework_SYS4U I&Csys4u
 
Nodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjsNodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjs기동 이
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기jongho jeong
 
.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기Seong Won Mun
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initializationEunjoo Im
 
Node.js 팀 스터디 발표자료.
Node.js 팀 스터디 발표자료.Node.js 팀 스터디 발표자료.
Node.js 팀 스터디 발표자료.SeungWoo Lee
 
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이  왜 이리 힘드나요?  (Lock-free에서 Transactional Memory까지)Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이  왜 이리 힘드나요?  (Lock-free에서 Transactional Memory까지)
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)내훈 정
 
웃으면서Python
웃으면서Python웃으면서Python
웃으면서PythonJiyoon Kim
 
Easy gameserver
Easy gameserverEasy gameserver
Easy gameserver진상 문
 
파이썬 스터디 15장
파이썬 스터디 15장파이썬 스터디 15장
파이썬 스터디 15장SeongHyun Ahn
 
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationSecrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationHyuncheol Jeon
 
ECS+Locust로 부하 테스트 진행하기
ECS+Locust로 부하 테스트 진행하기ECS+Locust로 부하 테스트 진행하기
ECS+Locust로 부하 테스트 진행하기Yungon Park
 
파이썬 병렬프로그래밍
파이썬 병렬프로그래밍파이썬 병렬프로그래밍
파이썬 병렬프로그래밍Yong Joon Moon
 
Node.js의 도입과 활용
Node.js의 도입과 활용Node.js의 도입과 활용
Node.js의 도입과 활용Jin wook
 

Similar to Gcd ppt (20)

C# Game Server
C# Game ServerC# Game Server
C# Game Server
 
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
시즌 2: 멀티쓰레드 프로그래밍이 왜이리 힘드나요?
 
[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성
 
Introduction to Fork Join Framework_SYS4U I&C
Introduction to Fork Join Framework_SYS4U I&CIntroduction to Fork Join Framework_SYS4U I&C
Introduction to Fork Join Framework_SYS4U I&C
 
Nodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjsNodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjs
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기
 
.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initialization
 
Node.js 팀 스터디 발표자료.
Node.js 팀 스터디 발표자료.Node.js 팀 스터디 발표자료.
Node.js 팀 스터디 발표자료.
 
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이  왜 이리 힘드나요?  (Lock-free에서 Transactional Memory까지)Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이  왜 이리 힘드나요?  (Lock-free에서 Transactional Memory까지)
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)
 
웃으면서Python
웃으면서Python웃으면서Python
웃으면서Python
 
pgday-2023
pgday-2023pgday-2023
pgday-2023
 
Easy gameserver
Easy gameserverEasy gameserver
Easy gameserver
 
JDK 변천사
JDK 변천사JDK 변천사
JDK 변천사
 
파이썬 스터디 15장
파이썬 스터디 15장파이썬 스터디 15장
파이썬 스터디 15장
 
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationSecrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
 
ECS+Locust로 부하 테스트 진행하기
ECS+Locust로 부하 테스트 진행하기ECS+Locust로 부하 테스트 진행하기
ECS+Locust로 부하 테스트 진행하기
 
파이썬 병렬프로그래밍
파이썬 병렬프로그래밍파이썬 병렬프로그래밍
파이썬 병렬프로그래밍
 
Node.js 기본
Node.js 기본Node.js 기본
Node.js 기본
 
Node.js의 도입과 활용
Node.js의 도입과 활용Node.js의 도입과 활용
Node.js의 도입과 활용
 

More from Sangon Lee

NoSQL Guide & Sample
NoSQL Guide &  SampleNoSQL Guide &  Sample
NoSQL Guide & SampleSangon Lee
 
Android ndk jni 설치및 연동
Android ndk jni 설치및 연동Android ndk jni 설치및 연동
Android ndk jni 설치및 연동Sangon Lee
 
Android xml parsing
Android xml parsingAndroid xml parsing
Android xml parsingSangon Lee
 
Naver api for android
Naver api for androidNaver api for android
Naver api for androidSangon Lee
 
Android 기초강좌 애플리캐이션 구조
Android 기초강좌 애플리캐이션 구조Android 기초강좌 애플리캐이션 구조
Android 기초강좌 애플리캐이션 구조Sangon Lee
 
번역돋보기 기획서
번역돋보기 기획서번역돋보기 기획서
번역돋보기 기획서Sangon Lee
 
Storyboard iOS 개발실습예제
Storyboard iOS 개발실습예제Storyboard iOS 개발실습예제
Storyboard iOS 개발실습예제Sangon Lee
 
17. cocos2d 기초
17. cocos2d  기초17. cocos2d  기초
17. cocos2d 기초Sangon Lee
 

More from Sangon Lee (10)

NoSQL Guide & Sample
NoSQL Guide &  SampleNoSQL Guide &  Sample
NoSQL Guide & Sample
 
Android ndk jni 설치및 연동
Android ndk jni 설치및 연동Android ndk jni 설치및 연동
Android ndk jni 설치및 연동
 
Android xml parsing
Android xml parsingAndroid xml parsing
Android xml parsing
 
Naver api for android
Naver api for androidNaver api for android
Naver api for android
 
Android 기초강좌 애플리캐이션 구조
Android 기초강좌 애플리캐이션 구조Android 기초강좌 애플리캐이션 구조
Android 기초강좌 애플리캐이션 구조
 
Facebook api
Facebook apiFacebook api
Facebook api
 
번역돋보기 기획서
번역돋보기 기획서번역돋보기 기획서
번역돋보기 기획서
 
StudyShare
StudyShareStudyShare
StudyShare
 
Storyboard iOS 개발실습예제
Storyboard iOS 개발실습예제Storyboard iOS 개발실습예제
Storyboard iOS 개발실습예제
 
17. cocos2d 기초
17. cocos2d  기초17. cocos2d  기초
17. cocos2d 기초
 

Recently uploaded

캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Wonjun Hwang
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionKim Daeun
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)Tae Young Lee
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Kim Daeun
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Wonjun Hwang
 

Recently uploaded (6)

캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
 

Gcd ppt

  • 1. Grand Central Dispatch S.O.Lab develop By oracleOn
  • 2. 흑마술 회전하는 마우스 커서 와 처리중 메세지 ‘ ’웹 기반 프로그래밍 분야에서 새로고침 백그라운드 프로세싱백그라운드 프로세싱 스레드스레드
  • 3. iOS Muti Tasking 멀티코어 프로세서의 등장 다중 Thread 프로그래밍 구현욕구 iPhone 3GS : S5PC100 600MHz : 싱글코어 / iPhone 4 : A4(4): 싱글 코어 iPhone 4s : A5 : 듀얼코어 / iPhone 5 : A6: : 듀얼코어
  • 4. Apple 의 계획 ? 의도 ? WWDC2009 처음 GCD 가 공개되는 순간 http://www.youtube.com/watch?v=nhbA9jZyYO4 GCC ==> LLVM ==> LLVM 2.0 LLVM 덕분에 가능해진 기술이 Block 과 GCD
  • 5. 메모리 관리와 Block 코딩 iOS 4.0 부터 멀티태스킹을 지원 메모리 부족문제를 GCD 를 이용하여 개선하고자 하였음 GCD 를 구현하기위한 Block 코딩 (GCD 는 Block-based API) Block : 코드자체 ( 데이터 , 함수 , 클래스 ) 를 객체화 할수 있는것
  • 6. ios Thread NSThread 실험 1 (addSubview 와 Thread) - (void)viewDidLoad { [super viewDidLoad]; _countlabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; _countlabel.text = @"0"; [self.view addSubview:_countlabel]; _goBt =[UIButton buttonWithType:UIButtonTypeCustom]; _goBt.frame = CGRectMake(100, 200, 100, 30); [_goBt setTitle:@"go Second page" forState:UIControlStateNormal]; [_goBt addTarget:self action:@selector(goSecondPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_goBt]; [NSThread detachNewThreadSelector:@selector(firstThread) toTarget:self withObject:nil]; } -(void) firstThread { for (int i=0; i<100; i++){ NSString *t = [NSString stringWithFormat:@"%i",[_countlabel.text intValue]+1]; [self performSelectorOnMainThread:@selector(mainThreadSetText:) withObject:t waitUntilDone:YES]; [NSThread sleepForTimeInterval:1.0]; } } -(void)mainThreadSetText:(NSString *)text { _countlabel.text = text; NSLog(@"count: %@",_countlabel.text); } -(void) goSecondPage { secondView *tempView = [[secondView alloc]init]; [self.view addSubview:tempView.view]; }
  • 7. ios Thread NSThread 실험 1 (addSubview 와 Thread) - (void)viewDidLoad { [super viewDidLoad]; _countlabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; _countlabel.text = @"0"; [self.view addSubview:_countlabel]; _goBt =[UIButton buttonWithType:UIButtonTypeCustom]; _goBt.frame = CGRectMake(100, 200, 100, 30); [_goBt setTitle:@"go Second page" forState:UIControlStateNormal]; [_goBt addTarget:self action:@selector(goSecondPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_goBt]; [NSThread detachNewThreadSelector:@selector(firstThread) toTarget:self withObject:nil]; } -(void) firstThread { for (int i=0; i<100; i++){ NSString *t = [NSString stringWithFormat:@"%i",[_countlabel.text intValue]+1]; [self performSelectorOnMainThread:@selector(mainThreadSetText:) withObject:t waitUntilDone:YES]; [NSThread sleepForTimeInterval:1.0]; } } -(void)mainThreadSetText:(NSString *)text { _countlabel.text = text; NSLog(@"count: %@",_countlabel.text); } -(void) goSecondPage { secondView *tempView = [[secondView alloc]init]; [self.view addSubview:tempView.view]; }
  • 8. ios Thread NSOperation * 전역변수에 두개이상의 스래드에서 동시에 접근하는 경우 * 많은 수의 스래드 생성이 발생하였을때 리소스와 퍼포먼스의 저하 1. 직접 큐를 만들어서 NSThread 갯수를 제어하는 방식 - NSMutableArray 로 큐용 배열를 만들어두고 스래드 호출하면 큐에 넣는다 . - 현재 돌고있는 스래드 수를 확인한후 설정한 최대의 스래드 수를 넘지 않으면 스래 드를 만들어서 진행한다 . - 전부 사용중일때는 하나가 끝날때 까지 기다린다 . NSOperation / NSOperationQueueNSOperation / NSOperationQueue 구현하기도 복잡하고 , 상황에 대처하여 제어하기 어렵다 .
  • 9. NSOperationQueue LIFO : 스택 / FIFO : 큐 NSOperationQueue 는 NSOperation 을 담는 큐이며 FIFO 방식으로 들어간 순서대로 NSOperation 을 실행 시켜 주는 기능 NSOperation 은 NSThread 를 만들때 직접 함수와 그 함수가 들어있는 객체 (target) 를 지정해주는것과 대조 NSOperation 은 실행 함수도 직접 자기자신으로 지정해두는것이 NSThread 와 다른점 #import <Foundation/Foundation.h> @interface TempOperation : NSOperation @property(assign, nonatomic) int countNum; @end #import "TempOperation.h" @implementation TempOperation -(void) main { NSLog(@"Start : %i",_countNum); [NSThread sleepForTimeInterval:1.0]; NSLog(@"End: %i",_countNum); } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; int i =0; [queue setMaxConcurrentOperationCount:2]; // 최대 큐수 TempOperation *test = [[TempOperation alloc]init]; test.countNum = i++; [queue addOperation:test]; test = [[TempOperation alloc]init]; test.countNum = i++; [queue addOperation:test]; test = [[TempOperation alloc]init]; test.countNum = i++; [queue addOperation:test]; test = [[TempOperation alloc]init]; test.countNum = i++; [queue addOperation:test]; NSLog(@"queue adding complete"); }
  • 10. NSInvocationOperation Class - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Load" style:UIBarButtonItemStyleDone target:self action:@selector(loadData)]; NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000]; self.array = _array; [_array release]; } - (void) loadData { /* Operation Queue init (autorelease) */ NSOperationQueue *queue = [NSOperationQueue new]; /* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; [operation release]; } - (void) loadDataWithOperation { NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"]; NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL]; for(NSString *str in tmp_array) { [self.array addObject:str]; } [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation You can use this class to initiate an operation that consists of invoking a selector on a specified object
  • 11. Grand Central Dispatch 1. NSOperationQueue 가 있듯이 GCD 에는 dispatch_queue_t NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; dispatch_queue_t dQueue = dispatch_queue_create(“com.apple.testQueue”, NULL); dispatch_queue_create dispatch_async(dQueue, ^{ ... }); // dispatch_async 는 큐에 블럭을 넣는 일을 하는 함수 dispatch_get_main_queue(); GCD 의 큐의 경우 무조건 한번에 하나만 실행 ~~!!!
  • 12. Grand Central Dispatch 메인큐는 메인스레드상에서 dispatch_get_main_queue() 직접 만든 큐는 하나의 스레드를 할당 받고 사용 dispatch_queue_create 글로벌 큐는 큐에 넣는대로 바로 스레드를 만들어서 실행 dispatch_get_global_queue(); #import "ViewController.h" #include <dispatch/dispatch.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. dispatch_queue_t dqueue = dispatch_queue_create("test01", NULL); __block int i =0; dispatch_async(dqueue, ^{ NSLog(@"GCD : %i",i); [NSThread sleepForTimeInterval:2.0]; NSLog(@"GCD %i END",i); i++; }); dispatch_async(dqueue, ^{ NSLog(@"GCD : %i",i); [NSThread sleepForTimeInterval:2.0]; NSLog(@"GCD %i END",i); i++; }); } [queue setMaxConcurrentOperationCount:1]; dispatch_queue_create // 최대큐를 1 개로 지정한것과 같다 . [queue setMaxConcurrentOperationCount:100]; dispatch_get_global_queue(); // 최대큐를 무한대까지 지정한 것과 같다 .
  • 13. Semaphore & Mutex Semaphore : 공유 리소스에 접근할 수 있는 최대 허용치만큼 동시에 사용자 접근을 할 수 있게하는 것 Mutex : 한 번에 하나의 쓰레드만이 실행되도록 하는 재 입장할 수 있는 코드 섹션에 직렬화된 접근을 허용하 는것 프로세스 간 메시지를 전송하거나 , 혹은 공유메모리를 통해서 특정 data 를 공유하게 될 경우 발생하는 문제는 , 공유된 자원에 여러 개의 프로세스가 동시에 접근 하면서 발생한다 . 단지 , 한번에 하나의 프 로세스만 접근 가능하도록 만들어 줘야 하고 , 이때 Semaphore 를 쓴다 . 뮤텍스는 값이 1 인 세마포어 리소스 Thread
  • 14. Semaphore & Mutex Dispatch_semaphore_create(3) 리소스 Thread 뮤텍스와 세마포어 .. 언떤 상황에서 어떻게 사용하는 것이 좋 은가 ?
  • 15. Grand Central Dispatch dispatch_semaphore_t Tsemaphore = dispatch_semaphore_create(3); dispatch_semaphore_wait ==> 먼저 하나의 블럭이 실행되면 wait 을 호출 ==> 내부 카운트를 1 올린다 . 그리고 내부 카운트가 먼저 생성할때 설정한 3 보다 적으면 바로 돌아가고 , 많을 경우 Sleep 을 걸어버린 다 . dispatch_semaphore_signal ==> 내부 카운트 1 내린다 . 그리고 다른 블럭에서 작업이 끝날때 signal 을 날려주면 내부 카운트가 하나 줄게된다 . 내부 카운트가 줄었을때 설정한 3 보다 적으면 재우던 스레드를 깨워서 진행 시킨다 . dispatch_semaphore_t Tsignal = dispatch_semaphore_create(2); //2 개 짜리 세마포어 생성 dispatch_async(dqueue, ^{ dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"GCD : %i 스레드 시작 ",i); [NSThread sleepForTimeInterval:2.0]; NSLog(@"GCD : %i 스레드 끝 ",i); dispatch_semaphore_signal(Tsignal); }); });
  • 16. Grand Central Dispatch - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. dispatch_queue_t dqueue = dispatch_queue_create("test01", NULL); //__block int i =0; int i =0; dispatch_semaphore_t Tsignal = dispatch_semaphore_create(2); //2 개 짜리 세마포어 생성 dispatch_async(dqueue, ^{ dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"GCD : %i 스레드 시작 ",i); [NSThread sleepForTimeInterval:2.0]; NSLog(@"GCD : %i 스레드 끝 ",i); dispatch_semaphore_signal(Tsignal); }); }); i++; dispatch_async(dqueue, ^{ dispatch_semaphore_wait(Tsignal, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"GCD : %i 스레드 시작 ",i); [NSThread sleepForTimeInterval:2.0]; NSLog(@"GCD : %i 스레드 끝 ",i); dispatch_semaphore_signal(Tsignal); }); }); i++; ........... }