is TBitmap thread safe?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
earik
Posts: 1
Joined: Tue Nov 10, 2020 2:15 pm

is TBitmap thread safe?

Post by earik »

Hi everyone,

I'm upgrading from a (very) old version of C++ Builder. In the old version, TBitmap wasn't threadsafe, and I had to use an alternate class to make this work. I remember reading that TBitmap was supposed to have been made thread safe, but am having trouble confirming if this is true or not. Does anyone happen to know? Would be nice to be able to ditch the 3rd party stuff if possible.

Thanks,

E
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: is TBitmap thread safe?

Post by rlebeau »

TBitmap can be *used* in a worker thread, but I wouldn't necessarily call it *thread-safe*. You still have to synchronize access to it, but you can do that by simply locking its Canvas while the TBitmap is in use. The main VCL thread manages GDI resources in the background, and globally keeps track of all TBitmap::Canvas HDCs that are allocated, and will periodically free "unused" HDCs that are not locked. Which, needless to say, can screw up a TBitmap in a worker thread if it is not locked when the main thread performs its sweeps. And putting a mutex or other sync object around the TBitmap is not sufficient to prevent this, the TBitmap::Canvas has its own internal lock that must be used.
Remy Lebeau (TeamB)
Lebeau Software
Post Reply