Multi-Threading for TBitmap, TCanvas, and TContext3D
Go Up to FireMonkey
FireMonkey supports multi-threaded use of TBitmap, TCanvas, and TContext3D. Bitmaps can be created, used, and deleted across any thread. Drawing on a canvas or 3D context is internally serialized; that is, while the code that draws can run on any thread, and several threads can draw at the same time, internally access is serialized such that only one thread can draw on a canvas or 3D context at once.
Contents
TBitmap
TBitmap
has complete multi-threading support. Instances can be created, destroyed, and modified in any thread without synchronization.
Note: You will still require normal thread synchronization if you share objects between threads, to ensure an object is not deleted while another thread is using it. Also, be cautious adding your own synchronization around bitmap access. It can cause deadlocks.
TCanvas
TCanvas
and all subclasses have support for multi-threaded environments, but does not support simultaneous execution of Canvas instances. This means that when a Canvas calls BeginScene
, the execution of the code inside this BeginScene...EndScene
section blocks any other thread that attempts to process drawing, including drawing on other canvases.
Write BeginScene...EndScene
blocks as short as possible to avoid blocking other threads.
For example, write:
for I := 0 to 10000 do
begin
// thread work
Canvas.BeginScene;
try
// drawing code
finally
Canvas.EndScene;
end;
end;
Instead of:
Canvas.BeginScene;
try
for I := 0 to 10000 do
begin
// thread code
// drawing code
end;
finally
Canvas.EndScene;
end;
TContext3D
TContext3D
has support for multi-threaded environments, but does not support simultaneous execution of Context3D instances. It has the same limitations as TCanvas
.