> 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)?
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
can find a way around that.
--
Jens Gruschel
http://www.pegtop.net