dispatch_a
dispatch_a
1 #include <stdio.h> 2 #include <dispatch/dispatch.h> 3 4 static long long 5 Sum( long long p = 1000000000 ) 6 { long long v = 0; 7 for ( long long i = 1; i <= p; i++ ) v += i; 8 return v; 9 } 10 11 void 12 Apply() 13 { 14 __block long long sum = 0; 15 16 dispatch_a pply 17 ( 10 18 , dispatch_g et_global_ queue( DISPATCH_Q UEUE_PRIOR ITY_DEFAUL T, 0 ) 19 , ^( size_t p ) 20 { printf( ">>%zd\n", p ); 21 sum += Sum(); 22 printf( "<<%zd\n", p ); 23 } 24 ); 25 26 printf( "sum = %lld\n", sum ); 27 } 28 29 void 30 GroupAsync () 31 { 32 __block long long sum = 0; 33 34 dispatch_g roup_t wG = dispatch_g roup_creat e(); 35 dispatch_q ueue_t wQ = dispatch_g et_global_ queue( DISPATCH_Q UEUE_PRIOR ITY_DEFAUL T, 0 ); 36 for ( int i = 0; i < 10; i++ ) 37 { dispatch_g roup_async 38 ( wG 39 , wQ 40 , ^() 41 { printf( ">>%zd\n", i ); 42 sum += Sum(); 43 printf( "<<%zd\n", i ); 44 } 45 ); 46 } 47 dispatch_g roup_wait( wG, DISPATCH_T IME_FOREVE R ); 48 dispatch_r elease( wG ); 49 printf( "sum = %lld\n", sum ); 50 51 } 52 53 void 54 Async() 55 { 56 __block long long sum = 0; 57 58 dispatch_q ueue_t wQ = dispatch_g et_global_ queue( DISPATCH_Q UEUE_PRIOR ITY_DEFAUL T, 0 ); 59 for ( int i = 0; i < 10; i++ ) 60 { dispatch_a sync 61 ( wQ 62 , ^() 63 { printf( ">>%zd\n", i ); 64 // sum += Sum(); 65 sleep( 7 ); 66 printf( "<<%zd\n", i ); 67 } 68 ); 69 } 70 dispatch_m ain(); 71 } 72 73
posted by
Saturn
on Fri 29 Nov 2013
at 00:24