I have a class which is a descendant of TThread. I have some public properties that are read only. Will I run into problems if my main thread reads those values while the thread is active?
If by "read-only properties" you mean the TThread descendant itself doesn't ever change them either, and initializes them a.s.a.p, then no, everything will be fine (as long as you make sure the thread is alive and kicking whenever you request the property values).
If by "read-only properties" you mean the TThread descendant will be the only one changing them, you will want to make sure the main thread isn't reading them while they are being changed (unless they are atomic values, like integers).
The basic types, like Integer
, Boolean
, Char
, and Pointer
, are safe to read at any time. Reference types, like string
, interfaces, and dynamic arrays, are safe to read only if there's no chance the other thread could be assigning a new value at the same time. Use a critical section or the Synchronized
method, for example, to make sure the thread isn't modifying the value while the main thread is reading from it.
You also have to remember that any value you read may be out of date by the time you use it — the thread might have written a new value between the time you read it and the time you use it.
That depends on the property types and possibly on their accessor methods.
type
TMyThread = class(TThread)
private
FIntfield: integer;
public
property IntField: integer read FIntField;
end;
Accessing this property won't be a problem since accessing 32 bit values is an atomic operation. But if the property is larger than 32 bit or a class reference that might be changed while the main thread accesses it, you will run into trouble.