Local Links

External Links


Search this site

Multiprocessing with REALbasic


This article shows how a REAL Studio application can make use of multiple CPUs on a Mac OS X computer in order to gain more processing power.

It comes with a demo project that can also be used as a framework for your own projects.

Threads versus Processes

REALbasic's multi-threading model is co-operative, with all its threads sharing just one CPU. If you have a computer with more than one CPU core, like most Intel based Macs and PCs do nowadays, a REALbasic app does not take advantage of their full capacity. And it's not likely that RB's threading model will change in the foreseeable future to enable the use of multiple CPUs or cores.

What can be done, however, is to use multiple processes. As former REAL Software employee and compiler engineer Mars Saxman suggested in his blog, processes are practically stand-alone applications, each with their own individual runtime environment. While they can not share their data objects directly as threads do, they're much safer and easier to handle when it comes to design and debugging.

This article shows how a REALbasic application can be turned into a multi-process application that is able to use all available CPUs concurrently.


First, some terms I will use in this article:

  • Controller (also called Host here) is the application that runs first and can choose to invoke more processes.
  • Workers (or Agents) are the processes (here: Console applications) invoked and controlled by the host, working silently in the background and exchanging data with the host.


There are a few difficulties that may come up:

  1. By default, RB does not let us invoke multiple instances of the same application.
  2. Passing data between Host and each Worker should be efficient and easy to use, and possibly allow to send arbitrary data, even from Memoryblocks. Files could be used but cause unnecessary disk access.
  3. Debugging is difficult because the processes started by the Controller must be pre-built apps, which means the REAL Studio debugger can't be used with them.


  1. First of all, it is necessary to have two separate projects to produce two separate types of applications: One for the Controller, handling all the user interaction by providing a GUI, the other for the console application that remains invisible to the user.
  2. The FolderItem.Launch function does not launch another copy of an app (worker) if it's already running. The work-around to this is to invoke the app with the Shell.Execute method.
  3. Using a Shell in Interactive mode makes it possible to exchange data (including commands and replies) between Host and Workers through the Shell's Write and ReadAll commands and the Worker's Input and Print commands. Additionally, a IPCSocket can be used for faster communication. The use of a Shell has one neat side effect: If the worker process should quit abnormally, e.g. due to a crash, this can be noticed immediately. Also, if the host app quits, all its workers are automatically quit as well by the OS.


A big challenge is debugging this kind of multi-processing in RB. The difficulty comes from the fact that RB's debugger will only talk to applications the IDE has launched itself. When our host app is launching its workers, those can't be used with RB's debugger, therefore.

We'll need to resort to using other means to debug the code, such as printing their progress to a file or to the console, or showing states with MsgBox calls etc.

Sample code

Without further explanation, here's something to play with:

It contains two samples: A simple just showing how commands are sent to 4 workers, and a sophisticated one for calculating a Mandelbrot picture.

With both demos, first build the Worker project, then run the Demo project from the IDE. The Mandelbrot demo allows you to change various options such as using IPC or not, and the number of processes.

It also contains a set of classes you could use in your own projects, as they take care of all the work around invoking and talking to the Worker processes.

If you need more information on how to use this, read the article in the March/April issue of REAL Studio Developer.

More Information

Get issue 9.3 of the RS Developer Magazine. It contains an article by its editor Marc Zeedar explaining the use of my classes in more detail.

Page last modified on 2013-09-27, 18:07 UTC (do)
Powered by PmWiki