Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
Like this article? We recommend

Concurrency

One of the nicest things you can do with this model is simply implement inter-thread messaging. The EtoileThread framework, part of EtoileFoundation, uses higher-order messaging to send messages between threads. To run a method in another thread, you just do this:

[[myObject inNewThread] doSomethingThatTakesALongTime];

The return value is yet another proxy, when you send it a message, it simply blocks until the operation completes, and then forwards the message to the result. If you reuse the same proxy, you can send it a series of messages that are all executed in order, in the second thread, and complete asynchronously. For example, we use this feature in a music player application that runs the decoder in a separate thread from the UI. The entire application contains only one line of threading-related code.

You can also use concurrency within other higher-order messaging operations. The map method, for example, might have a variant like this:

[[anArray mapInThreads: 2] someMessageWith: anArgument];

This would spawn two threads, each of which would iterate over half of the array, and then the two returned arrays would be joined together. If you also implement a concurrent fold operation, then you can use parallel map-reduce on any of your existing Objective-C objects with no effort.

  • + Share This
  • 🔖 Save To Your Account