Article

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

Re: How threadsafe is TBitmap

> It appears to be threadsafe, each thread processing his own index. I 
> have tested this code very often. However, very rarely I get a illegal 
> memory read/write. Are there things I overlook? Should I read some 
> articles (i couldn't find them)?

TBitmap itself is not thread-safe. However the (DIB) pixel data can be accessed 
from various threads without problems, after all it's just some memory block. Of
course your algorithm itself must be synchronized, for example if several 
threads are accessing the same pixel (it's no problem if each thread works on a 
different slice of the pixel data, or if all threads only read from the pixel 
data). And you must make sure that no other thread resizes the bitmap during 
your operation (which results in freeing the memory block and recreating a new 
one). Usually I don't pass the bitmap object itself to the processing threads, 
but only a pointer to the pixel data, so the thread doesn't know about the 
TBitmap at all, like this:

var    Origin: Pointer;    Pitch: Integer; begin    Origin := Bitmap.ScanLine[0];    Pitch := Integer(Bitmap.ScanLine[1]) - Integer(Origin);    for I := 0 to ThreadCount - 1 do begin      MyThreads[I].Init(Origin, Pitch, Bitmap.Width, Bitmap.Height);      MyThreads[I].StartProcessing;    end;    WaitForAllThreads;
Now to get a pointer to any scanline following formula can be used:
P := Pointer(Integer(Origin) + Pitch * Y);
The only problem with the code above is that it's not prepared for a 64 bit
compiler (having 64 bit pointer types and 32 bit integer types). But I guess you
can find a way around that.

-- 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:21:17 UTC
Copyright © 2009-2024
HREF Tools Corp.