Local Links External Links Contact Search this site |
Multiprocessing with REALbasicAboutThis 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 ProcessesREALbasic'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. TermsFirst, some terms I will use in this article:
ChallengesThere are a few difficulties that may come up:
Solutions
DebuggingA 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 codeWithout 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 InformationGet 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. |