I wonder if anything in my code is not thread safe as I get the occasional random freezes in the copy-background code (bitblt functions) when using multiple threads.
As you can see, all the bitmaps are created within the thread's execute function and their source data is coming from bitmaps in the main thread that is copied over within a critical section.
This is using Delphi 7.
procedure TThumbRendererThread.Execute;
var
thumbBitmapDarkL : TBitmap;
backgroundBitmap : TBitmap;
WorkBM : TBitmap;
begin
thumbBitmapDarkL := TBitmap.Create;
backgroundBitmap := TBitmap.Create;
WorkBM := TBitmap.Create;
WorkBM.Width := thumbWidth;
WorkBM.Height := thumbHeightTotal;
BackdropChanged := True;
While (Terminated = False) do
Begin
If BackdropChanged = True then
Begin
csThumbRenderer.Enter;
Try
// Copy bitmaps from main thread (within Critical Section)
thumbBitmapDarkL.Assign(LibNavForm.thumbBitmapDark);
backgroundBitmap.Assign(s_mlBackground);
Finally
csThumbRenderer.Leave;
End;
BackdropChanged := False;
End;
DebugLog('Copy backgrounds (before)');
BitBlt(WorkBM.Canvas.Handle,0,0,thumbWidth,thumbHeight,thumbBitmapDarkL.Canvas.Handle,xOfs,yOfs,SRCCOPY);
BitBlt(WorkBM.Canvas.Handle,0,thumbHeight,thumbWidth,thumbHeightTotal-thumbHeight,BackgroundBitmap.Canvas.Handle,xOfs,yOfs+thumbHeight,SRCCOPY);
DebugLog('Copy backgrounds (after)');
// Do other things
End;
WorkBM.Free;
backgroundBitmap.Free;
thumbBitmapDarkL.Free;
end;