[Lazarus] Threads in Lazarus code base

Juha Manninen (gmail) juha.manninen62 at gmail.com
Fri Sep 17 10:29:23 CEST 2010


Ok, I used Application.AddOnIdleHandler for the task instead of a thread.

First I tried TIdleTimer first but it is confusing.
Sometimes OnStartTimer handler was called first, sometimes OnTimer.
What does Interval do when the context is idle time?
What about AutoEnabled versus Enabled?

Now the form shows immediately but the controls on the form do not.
It is all grey while the scan finction is running. Anyway, it is good enough, 
just not as good as the thread solution, and the implementation is clumsy.

I understood that FPC thread support is not perfect.
That is the only valid reason not to use threads in this case.
And why is a thread the best solution here?
Because there is no interaction between the tasks, except for the WaitFor in 
the end! Yes, there are such cases. OS kernel has an advanced system to give 
time slices to GUI thread when needed. It is stupid not to use this clever 
system.

A good general rule for any thread usage is:
As little interaction as possible!
If you need lots of interaction then don't use threads or change your design. 
For example a parallel loop is a perfect solution when the threads can run 
freely and finally the results are combined somehow.

I have (almost) only used "Synchronize" and "WaitFor" methods for thread 
interaction. Not because I was sloppy but because it was enough for my 
threads.

I honestly wonder the attitude against threads here. I hear comments of Pascal 
being a dying language from the past. Maybe it is true after all.
How can one take seriously a developer community which claims threading is 
inherently wrong and bad?

BTW, TThread class has a good and intuitive interface, IMO. A new language 
syntax for parallelism would be nice but it wouldn't make any fundamental 
difference. Start a thread and call WaitFor later. How much easier can it be?

Juha

P.S.
I have the thread implementation of the cache function saved and waiting for 
better times.




More information about the Lazarus mailing list