Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/2999b01c68154453cd82db6a902b7360 to your computer and use it in GitHub Desktop.
Save jacobsapps/2999b01c68154453cd82db6a902b7360 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
printf("[Main Thread] Application started.\n");
printf("[Main Thread] Initiating a heavy task on a background queue...\n");
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{
// runs on background
printf("[Background Thread] Starting simulated heavy computation...\n");
// simulate long-running work
for (int i = 1; i <= 3; i++) {
[NSThread sleepForTimeInterval:1.0];
printf("[Background Thread] ...processing step %d of 3...\n", i);
}
printf("[Background Thread] Heavy computation finished.\n");
dispatch_async(dispatch_get_main_queue(), ^{
printf("[Main Thread] UI update: Background task completed successfully!\n");
// update UI from main queue
});
});
printf("[Main Thread] Main thread is free and can continue doing other work.\n");
// Keep the main thread's run loop alive for a few seconds.
// In a full Cocoa application (e.g., iOS or macOS app), the main application
// run loop would automatically keep the program alive. For a command-line tool,
// we manually run it to allow the asynchronous tasks time to complete.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:4.0]];
printf("[Main Thread] Application ending.\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment