Untitled

 avatar
unknown
plain_text
12 days ago
1.2 kB
3
Indexable
#import <Foundation/Foundation.h>

void runMainRunLoop() {
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Block 1: Starting a long task...");
            
            NSTask *task = [[NSTask alloc] init];
            task.launchPath = @"/bin/sleep"; // Simulating a long task
            task.arguments = @[@"5"];
            [task launch];

            [task waitUntilExit]; // This will block the current block
            NSLog(@"Block 1: Task completed.");
        });

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Block 2: Executing while Block 1 is waiting.");
            for (int i = 0; i < 5; i++) {
                NSLog(@"Block 2: Iteration %d", i);
                [NSThread sleepForTimeInterval:1]; // Simulate work
            }
        });

        NSLog(@"Starting main run loop...");
        runMainRunLoop(); // Keeps the main thread active and processing events
    }
    return 0;
}
Leave a Comment