Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //other necessary includes
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <dispatch/dispatch.h>
  5.  
  6. typedef void (^voidBlock)(void);
  7. typedef int (^intBlock)(int);
  8.  
  9. void doAsyncWork(voidBlock theWork) {
  10.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  11.     dispatch_async(queue, block);
  12. }
  13.  
  14. int main(int argc, char** argv) {
  15.     dispatch_semaphore_t workDone = dispatch_semaphore_create(0);
  16.     doAsyncWork( ^ {
  17.         //something expensive
  18.         dispatch_semaphore_signal(workDone);
  19.     });
  20.     dispatch_semaphore_wait(workDone);
  21.     dispatch_release(workDone);
  22.     printf("yay work finished!\n");
  23.     return EXIT_SUCCESS;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement