Recent

Author Topic: Multithreaded filter exception  (Read 373 times)

lainz

  • Hero Member
  • *****
  • Posts: 3398
    • Lainz
Multithreaded filter exception
« on: January 11, 2020, 01:56:59 pm »
Hi, I'm trying to create a multithreaded filter with BGRABitmap.

For testing purpouses I'm implementing a simple grayscale filter with 2 threads.

https://github.com/bgrabitmap/multithreadedfilter

It works, but the problem I have is that I get an exception when I resize the form a lot of times

This is the algorithm unit
https://github.com/bgrabitmap/multithreadedfilter/blob/master/umultithreadfilter.pas

Code: Pascal  [Select]
  1. arr[i] := TThreadFilter.Create(True);

Attached the exception dialog.

In English is something like:

"There are not enough memory resources to process this command".
« Last Edit: January 11, 2020, 04:46:00 pm by lainz »

MarkMLl

  • Hero Member
  • *****
  • Posts: 601
Re: Multithreaded filter exception
« Reply #1 on: January 11, 2020, 02:33:14 pm »
Check that your thread's code isn't trying to manipulate the GUI directly (you need to use Synchronize()). If you've got custom code drawing to e.g. a canvas, check that it's all in the repaint handler since otherwise it will go recursive.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

lainz

  • Hero Member
  • *****
  • Posts: 3398
    • Lainz
Re: Multithreaded filter exception
« Reply #2 on: January 11, 2020, 03:03:10 pm »
I added

Code: Pascal  [Select]
  1.   repeat
  2.     AllFinished := True;
  3.     for j := Low(arr) to High(arr) do
  4.       if arr[j] <> nil then
  5.         if not arr[j].AFinished then
  6.           AllFinished := False;
  7.   until AllFinished;

And now it works, but the main form is a bit slower... since the repeat is being executed in the main thread, so I lost the "visible" speed gain...

Code: Pascal  [Select]
  1. {for j:=Low(arr) to High(arr) do
  2.     arr[i].WaitFor;}

For some reason .WaitFor doesn't works, and the main form is never repainted.

lainz

  • Hero Member
  • *****
  • Posts: 3398
    • Lainz
Re: Multithreaded filter exception
« Reply #3 on: January 11, 2020, 04:46:48 pm »
I fixed the 'WaitFor' issue  :)

The problem was that I was calling Execute instead of Start. Now it works properly.

With 2 threads the code works nicely.

circular

  • Hero Member
  • *****
  • Posts: 3078
    • Personal webpage
Re: Multithreaded filter exception
« Reply #4 on: January 11, 2020, 06:09:58 pm »
Well done!  8-)
Conscience is the debugger of the mind