Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3816

Preemptive Pitfalls

$
0
0

When Preemptive Threads arrive, the first, best advice I can give you is…

Don’t. Use. Them.

If all you need is for your GUI to remain responsive while something happens in the background that doesn’t take that much time, you’ll be better off with the existing cooperative threads.

If you do have a compelling reason, you will have to consider things that, frankly, you’ve never had to consider in Xojo before. Unless you’ve had experience with preemptive threads in other languages, you might think you’re ready.

Trust me, you’re not.

Consider this seemingly straightforward pseudo-code:

do
  if getTheNextDataToProcess then
    processData
    continue loop
  end if

  if noMoreDataIsComing then
    exit loop
  end if
loop

This looks for data and, if none is there and no more is expected, we can declare it done and exit. This would work just fine in a cooperative thread.

Simple, right?

Not so fast. Because this is preemptive, things can happen simultaneously or within nanoseconds of something else. In this code, unless you’ve taken precautions, you could have added data, then told the thread that no more was coming after the getTheNextDataToProcess call, but before it checks noMoreDataIsComing.

The loop would find no data then exit because it thinks it’s done, even though more data was added in between.

Xojo will provide tools to help mitigate these issues, but these are still things you’ll have deal with.

And since this pseudo-code represents an actual failure that took two days to track down, believe me when I say, it won’t be fun.

25 posts - 13 participants

Read full topic


Viewing all articles
Browse latest Browse all 3816

Trending Articles