2012-06-26 2 views
3

대기열 유형 간의 차이점을 이해하려고합니다. - 동시 - 시리얼 - - 블록은 즉시 관계없이 순서 GCD 대기열 유형

  • 홈페이지 큐 가능한 실행되는 블록들이
  • 개인 제출 된로 실행됩니다

    • 글로벌 큐 : 나는 그것을 알고있는 것처럼 3 종류가 있습니다 큐 - 시리얼

    궁금한 점은 : 각 종류의 큐에 제출할 때 dispatch_sync와 dispatch_async의 차이점은 무엇입니까?

    dispatch_sync(global_queue)^ 
    { 
        // blocks are executed one after the other in no particular order 
        // example: block 3 executes. when it finishes block 7 executes. 
    } 
    
    dispatch_async(global_queue)^ 
    { 
        // blocks are executed concurrently in no particular order 
        // example: blocks 2,4,5,7 execute at the same time. 
    } 
    
    dispatch_sync(main_queue)^ 
    { 
        // blocks are executed one after the other in the order they were submitted 
        // example: block 1 executes. when it finishes block 2 will execute and so forth. 
    } 
    
    dispatch_async(main_queue)^ 
    { 
        // blocks are executed concurrently in the order they were submitted 
        // example: blocks 1-4 (or whatever amount of threads the system can handle at one time) will fire at the same time. 
        // when the first block completes block 5 will then execute. 
    } 
    

    나는이 내 인식이 올바른 얼마나 알고 싶습니다 : 이것은 내가 지금까지 그것을 이해하는 방법이다.

  • 답변

    2

    dispatch_sync와 dispatch_async의 차이점은 Windows API에서 sendmessage 및 postmessage와 비슷합니다. dispatch_sync 디스패치 대기열에서 실행을 위해 블록 객체를 제출하고 해당 블록이 완료 될 때까지 대기합니다. dispatch_async 디스패치 대기열에서 비동기 실행을 위해 블록을 제출하고 즉시 반환합니다.

    대상 큐는 블록이 동일한 큐에 제출 된 다른 블록과 관련하여 순차적으로 또는 동시에 호출되는지 여부를 결정합니다. 독립적 인 직렬 대기열은 서로에 대해 동시에 처리됩니다.

    정말 질문을 게시하기 전에 먼저 문서를주의 깊게 읽어야합니다.