Board index » cppbuilder » About locking canvas in secondary thread

About locking canvas in secondary thread


2007-12-20 03:45:34 PM
cppbuilder22
Hi all.
There were some posts in QC group about reports
Report #: 43018 Status: Closed
TBitmap.Draw is unsafe in multithreading apps
qc.codegear.com/wc/qcmain.aspx
and
Report #: 55871 Status: Open
TJPEGImage.Draw() is not thread safe
qc.codegear.com/wc/qcmain.aspx
The #43018 problem:
ACanvas.Draw(0, 0, bitmap);
is not thread safe (even if ACanvas is locked) and in addition we must
lock bitmap:
bitmap.Canvas.Lock;
try
ACanvas.Draw(0, 0, bitmap)
finally
bitmap.Canvas.Unlock;
end;
Report is closed ("Test case error", de facto "As designed").
But there is contradiction even from the formal point of view:
documentation says only about locking of canvas, but Draw gets TBitmap
as parameter. In other words I should do some assumptions about
Draw internals and lock bitmap.canvas myself.
At present for safety I lock all canvases of all objects in secondary
thread when I do any operation (with canvas, scanline, etc) even if I
don't touch these objects from other threads.
(for example, in thread I do as follows
new Bitmap
Bitmap->Canvas->Lock
... operation with Bitmap ...
Bitmap->Canvas->Unlock
delete Bitmap
)
Is this redundant ? And if yes - when I exactly must do locking ?
TIA.
--
Alex
 
 

Re:About locking canvas in secondary thread

"AlexB" < XXXX@XXXXX.COM >wrote:
Quote

There were some posts in QC group about reports [...]
I am aware that the docs for TCanvas::Lock 'seem' to
indicate that it's thread-safe but to be clear, the VCL
is *not* thread-safe.
Quote
At present for safety I lock all canvases of all objects in
secondary thread when I do any operation (with canvas,
scanline, etc) even if I don't touch these objects from
other threads.

(for example, in thread I do as follows
new Bitmap
Bitmap->Canvas->Lock
... operation with Bitmap ...
Bitmap->Canvas->Unlock
delete Bitmap
)

Is this redundant ?
That would depend on what operation you're doing with the
TBitmap (and in which thread context it's executing if it's
even safe). AFAIK, only TWinControl::PaintTo requires the
TBitmap::Canvas be locked.
~ JD