[DUG] A Super Simple Summary of Thread Safe
Conor Boyd
lists at conor.org.uk
Tue Jun 21 08:20:12 NZST 2005
This article might be of interest to a few people (or a link to file away
for future reference anyway) - it's a fairly detailed look at Delphi
multi-threading, which was put together by one of the guys in the Delphi
Usenet newsgroups a while ago with a fair bit of input from others in that
group:
http://www.pergolesi.demon.co.uk/prog/threads/ToC.html
Cheers,
C.
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
Behalf Of Dennis Chuah
I'll try not to be a cheer leader ...
1. Reading and writing to memory is *DEFINITELY* not thread safe, however
..
a. Reading from a memory location that is never written to after it has
been initialised is thread safe (just remember to bracket the initialisation
code with a critical section).
b. If you want to share memory, consider using thread safe classes such as
TThreadList.
2. The whole VCL (not just screen controls) is not thread safe. You think
that you have gotten away with threaded access to some parts, but bugs will
eventually show up, and they will be very hard to debug, and if you get an
AV, the stack trace will most probably not show you where the bug is.
Example problem: We have a piece of code that generates metafiles to disk.
It needs a window handle, and so it creates a hidden TForm object and uses
its handle. This is all done on a worker thread, ie., not the main thread.
The code works fine 99.999% of the time, but occasionally, we get a list
index out of bounds error. After a lot of debugging, I finally managed to
track down the problem to TScreen. The list error happens when the form is
freed, and TScreen is in the middle of updating a list of forms, the for
loop runs past the end! In this sense, it is not only not thread safe, it
is thread deadly! Actually, the problem is the VCL is not re-entrant safe.
3. Classes such as TStringList, TStream, TList, TCollection are not thread
safe, but OK if you only use them from one thread. Beware of TStrings - it
is an abstract class and some of the subclasses may not be thread safe - eg.
if the subclass is the Lines of a TMemo or Items of a TListbox / TCombobox.
[snip]
More information about the Delphi
mailing list