Running code concurrently -- Running code while running other code

suggest change

Say you want to perform in action (in this case, logging “Foo”), while doing something else (logging “Bar”). Normally, if you don’t use concurrency, one of these actions is going to be fully executed, and the other run will run only after it’s completely finished. But with concurrency, you can make both actions run at the same time:

dispatch_async(dispatch_queue_create("Foo", DISPATCH_QUEUE_CONCURRENT), ^{

for (int i = 0; i < 100; i++) { NSLog(@“Foo”); usleep(100000); }


for (int i = 0; i < 100; i++) {

NSLog(@“Bar”); usleep(50000);


This will log “Foo” 100 times, pausing for 100ms each time it logs, but it will do all this on a separate thread. While Foo is being logged, “Bar” will also be logged in 50ms intervals, at the same time. You should ideally see an output with “Foo”s and “Bars” mixed together

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents