Sunday, 16 March 2008

Threads - Cooperative and Preemptive

Lots of developers talk about multiple processors in the same dubious
way they've always spoken of bigger screens and higher screen resolutions.

If the user happens to have more, how can I fill it up?
All those resources aren't there so the computer can always run one app more greedily. In general, they're about allowing a richer multi-tasking experience.

I admit that a few apps will benefit from more processing power than one processor can provide but it's not a good idea to rely on high-end CPUs for most programs in any language.

REALbasic "threads" are not preemptive thread such as programmers coming from other environments might expect, they are cooperative threads which some people are surprised to learn are still exposed by the various operating systems.

We'll ignore the argument that what Microsoft call "fibres" (I can't bring myself to use MS spelling) might not really be entitled to the name "thread" on Windows any more.

There are ways (MBS is one but not the only) of attempting to use preemptive threads with RB but RS engineers assure us there is no safe way to use the framework with preemptive threads that does not effectively cripple them back to cooperative.

I don't try to use preemptive threads in RB because I believe it may eventually give crashes or (possibly worse) unpredictable results.
(This is a very slightly stronger statement than I make about using App.DoEvents in a gui app. I might try that in one particular set of circumstances.)

Sometimes to get around the lack of preemptive threads and still keep the gui responsive, I have to split long external reads up into shorter chunks. This can sometimes be a pain, especially with databases.

Sometimes I have to split out lengthy tasks into a helper app. If that is also a RB app, it significantly increases the size of my app's footprint on the disk.

I'd love to see preemptive threads in RB but my guess is
  1. we won't see them for a long time
  2. they'll take an even longer time to settle down when they do arrive
  3. they won't really make much difference to me
And of course there are many fixes and lots of new features (and their fixes) I'd like to see first.

No comments: