Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effective Multithreading in iOS

Effective Multithreading in iOS

Vladimir Pouzanov

March 26, 2011
Tweet

More Decks by Vladimir Pouzanov

Other Decks in Programming

Transcript

  1. Большое спасибо Майку Эшу за прекрасный список статей про GCD,

    и вообще за Friday Q&A http://www.mikeash.com/pyblog Monday, September 24, 12
  2. Зачем нам потоки? ✤ .'(*,% API )#%/(01"-$231 -(4+(5'(01& -6)($'21& 4"/"7,

    "0,'8#(''( ✤ NSURLConnection ✤ NSXMLParser ✤ .'(*,% API '%2-'( -6)($'231 #"9(1: - )"#"$$%$&'68 )(1(;"8 ✤ UIWebView ✤ MKMapView Monday, September 24, 12
  3. Grand Central Dispatch ✤ API /$2 )"#"$%$&'(*( )#(*#"++,#(-"',2 ✤ !(8(5

    '" NSOperationQueue ✤ !(+,+( )"#"$$%$&'(*( -6)($'%',2 9$(;(- ;(/" :+%%1 #%"*,#(-"1& '" -8(/2<,% 0(961,2 ✤ !(4-($2%1 -4*$2':1& '" )#,$(5%',% 9($%% *$(9"$&'( ✤ «=01#(-" 0%#,"$,4">,, - +(#% )"#"$$%$,4+"» Monday, September 24, 12
  4. Зачем вообще использовать GCD? ✤ GCD ?@@%;1,-'( #"9(1"%1 - +'(*(4"/"7'(+

    (;#:5%',,, ,0)($&4:2 #%0:#06, %0$, (', /(01:)'6, , +,',+,4,#:2 -67,0$%',2, %0$, 0,01%+" 4"'21" /#:*,+, 4"/"7"+, ✤ API (7%'& )#(01, ,0)($&4(-"',% 9$(;(- )(4-($2%1 ),0"1& ";;:#"1'( 01#:;1:#,#(-"''6A ;(/ ✤ GCD ?@@%;1,-%' /$2 B,#(;(*( 0)%;1#" 4"/"7 Monday, September 24, 12
  5. Базовые понятия: очереди ✤ =0'(-'"2 (7%#%/& (dispatch_get_main_queue()) ✤ C$(9"$&'6% (7%#%/,

    (dispatch_get_global_queue(), /(01:)'6 /$2 ',4;(*(, 0#%/'%*( , -60(;(*( )#,(#,1%1(-) ✤ =01"$&'6% (7%#%/,, ;(1(#6% 0(4/"%1 )#(*#"++,01 (dispatch_queue_create()) Monday, September 24, 12
  6. Базовые понятия: очереди ✤ !(0$%/(-"1%$&'6% Задача Задача Задача Задача ✤

    !"#"$$%$&'6% (7%#%/, Задача Задача Задача Задача Задача Задача Monday, September 24, 12
  7. dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); // [1] dispatch_async(q, ^{ %

    // ... [2] }); // [3] Постановка задачи в очередь ✤ dispatch_async – )%#%/"7" 9$(;" - (7%#%/& '" -6)($'%',% (@:';>,2 -(4-#"<"%102 0#"4:) Monday, September 24, 12
  8. dispatch_queue_t q = dispatch_get_main_queue(); // [1] dispatch_sync(q, ^{ % //

    ... [2] }); // [3] Постановка задачи в очередь ✤ dispatch_sync – )%#%/"7" 9$(;" - (7%#%/& '" -6)($'%',% , 9$(;,#(-;" /( 4"-%#B%',2 #"9(16 ✤ D 0$:7"%, %0$, (7%#%/& /$2 9$(;" ?;-,-"$%'1'" 1%;:<%A – )($:7"%+ :/(9'6A +%8"',4+ 9$(;,#(-;, )"#"$$%$&'68 4"/"7 Monday, September 24, 12
  9. dispatch_sync в деталях ✤ =7%'& ?@@%;1,-'"2 @:';>,2 ✤ E%0;($&;( 01#"1%*,A,

    - 4"-,0,+(01, (1 :0$(-,A #"9(16 ✤ F"+%'2%1 (967'6A locking-+%8"',4+ ✤ G"9(1"%1 ;"; '" )(0$%/(-"1%$&'68, 1"; , '" )"#"$$%$&'68 (7%#%/28 Monday, September 24, 12
  10. Но есть же традиционные блокировки! ✤ GCD - (/': 9:;-:

    )%#%8(/,1 (1 )(0$%/(-"1%$&'(*( ; )"#"$$%$&'(+: ✤ D6 ',;(*/" '% 4"9:/%1% (1;#61& NSLock ✤ =7%#%/, +(5'( $%*;( )#,(01"'"-$,-"1&, -(4(9'(-$21&, , )%#%/"-"1& 4"/"7, +%5/: ',+, Monday, September 24, 12
  11. Зависимости и группы задач ✤ H"01( -(4',;"3<"2 0,1:">,2: %01& ;(/,

    ;(1(#6A +(5'( #"0)"#"$$%$,1&, '( '%(98(/,+( -6)($',1& )(01-(9#"9(1;: ✤ F"/"7: '%$&42 #%B,1& 0 )(+(<&3 dispatch_async, " dispatch_sync 0-(/,1 '" '%1 -0% )#%,+:<%01-" '%0;($&;,8 -67,0$,1%$&'68 2/%# Monday, September 24, 12
  12. dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); for(id

    obj in array) % dispatch_group_async(group, q, ^{ % % // ... [1] % }); dispatch_group_wait(group, DISPATCH_TIME_FOREVER); dispatch_release(group); // [2] Зависимости и группы задач ✤ =9<,% 9$(;, +(5'( 0(9#"1& - *#:)): 4"/"7 ✤ E%0;($&;( -"#,"'1(- /%A01-,A: ✤ =5,/"1& 4"-%#B%',2 -0%A *#:))6 Monday, September 24, 12
  13. dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); for(id

    obj in array) % dispatch_group_async(group, q, ^{ % % // ... [1] % }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ % // ... [2] % dispatch_release(g); }); Зависимости и группы задач ✤ =9<,% 9$(;, +(5'( 0(9#"1& - *#:)): 4"/"7 ✤ E%0;($&;( -"#,"'1(- /%A01-,A: ✤ I(9"-,1& 9$(;, ;(1(#6A -6)($',102 )( 4"-%#B%',3 *#:))6 Monday, September 24, 12
  14. dispatch_set_target_queue(subQueue, queue); dispatch_suspend(queue); // [1] Строим цепочки ✤ =7%#%/,, 0(4/"''6%

    -#:7':3, )( :+($7"',3 )()"/"31 - *$(9"$&':3 (7%#%/& 0#%/'%*( )#,(#,1%1" ✤ =7%#%/, +(5'( )%#%+%<"1& - /#:*,% (7%#%/,, 1%+ 0"+6+ 0(4/"-"2 '%(98(/,+:3 01#:;1:#: 0-24%A ✤ !#, ?1(+ ;"5/:3 (7%#%/& +(5'( '%4"-,0,+( )#,(01"'(-,1& ,$, -(4(9'(-,1& Monday, September 24, 12
  15. Полезные мелочи ✤ J%+"@(#6 GCD #"9(1"31 1"; 5%, ;"; ,

    $396% /#:*,% 0%+"@(#6 ✤ dispatch_semaphore_create ✤ dispatch_semaphore_signal ✤ dispatch_semaphore_wait ✤ dispatch_once – -6)($'2%1 9$(; ;(/" (/,' #"4, +";0,+"$&'( ?@@%;1,-%' Monday, September 24, 12
  16. dispatch_queue_t a = dispatch_queue_create(“com.myqueue.a”, NULL); dispatch_queue_t b = dispatch_queue_create(“com.myqueue.a”, NULL);

    dispatch_sync(a, ^{ % dispatch_sync(b, ^{ % % dispatch_sync(a, ^{ % % % % % }); % }); }) Проблемы ✤ D4",+'"2 9$(;,#(-;" (7%#%/%A 7%#%4 dispatch_sync ✤ !#(,08(/,1 1($&;( '" )(0$%/(-"1%$&'68 (7%#%/28 Monday, September 24, 12
  17. dispatch_queue_t a = dispatch_queue_create(“com.myqueue.a”, NULL); dispatch_sync(a, ^{ % dispatch_sync(a, ^{

    % % printf(“Ohai, deadblock!\n”); % }); }) Проблемы ✤ D4",+'"2 9$(;,#(-;" (7%#%/%A 7%#%4 dispatch_sync ✤ !#(,08(/,1 1($&;( '" )(0$%/(-"1%$&'68 (7%#%/28 Monday, September 24, 12
  18. Проблемы ✤ K$(;,#(-;" #%0:#0(- / '%8-"1;" #%0:#0(- ✤ L4-4" +%/$%''(*(

    /,0;(-(*( (,$,, 1%+ 9($%%, 0%1%-(*() I/O, GCD +(5%1 0(4/"-"1& '(-6% , '(-6% #"9(7,% )(1(;,, ;(1(#6% 9:/:1 1($&;( :8:/B"1& )($(5%',% ✤ G%B%',%: 4")#(06 '" 08(5,% #%0:#06 /($5'6 )#(8(/,1& 7%#%4 (/': )(0$%/(-"1%$&':3 (7%#%/& Monday, September 24, 12
  19. Проблемы ✤ K$(;,#(-;" #%0:#0(- / '%8-"1;" #%0:#0(- ✤ L4-4" 9($&B(*(

    ;($,7%01-" )#(>%00(- +(5%1 '"7"102 0-()),'* (OSX, FreeBSD), ,$, )#,$(5%',% 9:/%1 :9,1( (iOS) ✤ G%B%',%: ,0)($&4(-"1& 0%+"@(# /$2 (*#"',7%',2 +";0,+"$&'(*( ;($,7%01-" #"9(7,8 )(1(;(- Monday, September 24, 12
  20. Проблемы ✤ !(1%#2 release-;('1%;01" /$2 UIView-,%#"#8,A , :/"$%',% ,8 -'%

    (0'(-'(*( )(1(;" ✤ !#(9$%+" 1,),7'" '% 1($&;( /$2 GCD ✤ L0)($&4(-"',% self ,$, $39(*( ivar - 9$(;% '%2-'( :-%$,7,-"%1 retain count ✤ !(0;($&;: 9$(; ;(),#:%102 , :/"$2%102 -( -'%B'%+ ;('1%;01% ()(1%'>,"$&'( – /#:*(+ )(1(;%), 1( , 0():101-:3<,% )%#%+%''6% +(*:1 961& (;('7"1%$&'( :/"$%'6 - /#:*(+ ;('1%;01% ✤ G%B%',%: MAZeroingWeakRef, :-%/(+$%',2 ,$, $39(A /#:*(A 0)(0(9 '%2-'(A )%#%/"7, ;('1%;01" '"4"/ Monday, September 24, 12