Created
November 21, 2012 05:36
-
-
Save kenji4569/4123263 to your computer and use it in GitHub Desktop.
simple block-based task queue with cascade and async trail processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dispatch_queue_t trail_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_group_t trail_group = dispatch_group_create(); | |
NSMutableArray *tasks = [[NSMutableArray alloc] init]; | |
void (^nextTask)() = ^() { | |
[tasks removeObjectAtIndex: 0]; | |
if ([tasks count] > 0) { | |
void (^task)() = [tasks objectAtIndex: 0]; | |
task(); | |
} else { | |
dispatch_group_notify(trail_group, trail_queue, ^{ | |
NSLog(@"finish"); | |
}); | |
dispatch_group_wait(trail_group, DISPATCH_TIME_FOREVER); | |
dispatch_release(trail_group); | |
} | |
}; | |
[tasks addObject: ^() { | |
NSLog(@"task1"); | |
dispatch_group_async(trail_group, trail_queue, ^{ | |
NSLog(@"task1 trail"); | |
}); | |
nextTask(); | |
}]; | |
[tasks addObject: ^() { | |
NSLog(@"task2"); | |
dispatch_group_async(trail_group, trail_queue, ^{ | |
NSLog(@"task2 trail"); | |
}); | |
nextTask(); | |
}]; | |
void (^task)() = [tasks objectAtIndex: 0]; | |
task(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment