Article

From:
To:
Arnold Reinders
Subject:
Re: How threadsafe is TBitmap
Newsgroup:
embarcadero.public.delphi.graphics

Re: How threadsafe is TBitmap

> Really, I'd like to see an official POV.

Officially you have to lock bitmaps, but not if the main thread is blocked :-)

The explanation is simple: you have to lock bitmaps unless you make sure FreeDeviceContexts is never called, which happens in TWinControl.DestroyWnd and TWinControl.MainWndProc. But these methods are only executed while the main (GUI) thread is active (processes messages etc.). I'd also avoid accessing any canvases or calling methods which could change a bitmap's state (resizing a bitmap is a no-no).
So you can: start a number of treads processing the pixel data (avoid using other methods or properties of TBitmap) if you make sure the main thread is blocked (use WaitForMultipleObjects with the handles of all threads or some events used by your threads signaling they have finished their work).
But you cannot: start one or more threads working with bitmaps and let the main thread continue (processing messages etc.) which could possibly free the device contexts while your threads are executed.
So basically you can parallelize your image processing tasks and make use of multi core CPUs, if you make sure it works like a normal method, returning when all the work is done. I'm doing this all the time. If you're interested I can post some code I'm using.
-- Jens Gruschel http://www.pegtop.net
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Fri, 15 Nov 2024 16:32:23 UTC
Copyright © 2009-2024
HREF Tools Corp.