Created
June 20, 2025 17:27
-
-
Save jacobsapps/2999b01c68154453cd82db6a902b7360 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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