Delphi Parallel programming - multi threading slow
delphi, delphi-xe2, multithreading, parallel-processing
Solution
MultiThreadding will not speed up your Media (HardDrive).
In fact it can slow down with parallel write access.
At first you have to measure if your Media (HardDisk) is able to store an image in less than 33ms - because every 33.333ms you will get a new image from webcam.
If not, you can't expect to get this running.
You should have to (and/or)
- use more hard disks (e.g. one per thread)
- use more cache (e.g. Cache Controller)
- use faster hard disks (e.g. SSD)
- use smaller images (decrease resolution)
- drop some images
And don't loose time if you need to be fast
if elElement<>NIL then
begin
JpegImg.Assign(elElement.bmWebcam) ;
JpegImg.SaveToFile('Save\' + elElement.sTime + '.jpg' );
elElement.Free;
end
else
Sleep(20);
OTL will not help to get your media faster, but would be much cleaner :o)
So you should have a look at
Problem
Good day, A webcam class has about 30 frames per second, and all of these frames will be saved in a vector(like a queue). Then 3 asynchronous threads will read the queue, and will try to do their jobs(to save these images). Why the queue is overflowing? So the problem is that these threads are slower than the webcam. ``` Procedure TSaveThread.Execute; begin while not terminated do begin elElement:=NIL; EnterCriticalSection(CritSect); if iElementsLength>=0 then begin elElement:=vElements[iElementsLength]; Dec(iElementsLength); end; LeaveCriticalSection(CritSect); if elElement<>NIL then begin JpegImg.Assign(elElement.bmWebcam) ; JpegImg.SaveToFile('Save\'+elElement.sTime+'.jpg') ; elElement.Free; end; Sleep(20); end; end; ``` Images added to the queue. ``` //------------------------------------------------------------------------------ Procedure TWebcam.OnSave(Sender:TObject; bmWebcam:TBitmap); begin EnterCriticalSection(CritSect); inc(iElementsLength); vElements[iElementsLength]:=TElement.Create(bmWebcam); LeaveCriticalSection(CritSect); end; ``` Creating the threads. ``` for i:=0 to 2 do TSaveThread.Create(false); ``` The thing is that, these threads are not able to save all these images. Why? How can I improve my threads? Delphi Version: Delphi XE2 Webcam frame size: 1280x760 or 960x600 Entire source code here: http://pastebin.com/8SekN4TE