A goroutine is a lightweight thread of execution. In Objective-C, we can use NSThread to achieve similar functionality. Here’s the translated code example:
Suppose we have a function call f(NSString *from). Here’s how we’d call that in the usual way, running it synchronously.
To invoke this function in a new thread, we use NSThread. This new thread will execute concurrently with the calling one.
You can also start a new thread for an anonymous function call.
Our two function calls are running asynchronously in separate threads now. Wait for them to finish.
When we run this program, we see the output of the blocking call first, then the output of the two threads. The threads’ output may be interleaved, because threads are being run concurrently by the Objective-C runtime.
Next we’ll look at other concurrency patterns in Objective-C programs.