Multithreading – Delphi parallel programming – multithreading slow

nice day,

The webcam class has about 30 frames per second, and all these frames will be saved in vectors (such as queues) Then three asynchronous threads will read the queue and will try to perform their work (to save the images) Why does the queue overflow? So the problem is that these threads are slower than webcams

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;

The image has been added to the queue

//------------------------------------------------------------------------------
Procedure TWebcam.OnSave(Sender:TObject; bmWebcam:TBitmap);
begin
   EnterCriticalSection(CritSect);
      inc(iElementsLength);
      vElements[iElementsLength]:=TElement.Create(bmWebcam);
   LeaveCriticalSection(CritSect);
end;

Create a thread

for i:=0 to 2 do
    TSaveThread.Create(false);

The problem is that these threads cannot save all these images Why? How can I improve my threads?

Delphi version: Delphi xe2

Webcam frame size: 1280 × 760 or 960 × 600 the entire source code here: http://pastebin.com/8SekN4TE

Solution

Multithreading will not speed up your media (harddrive)

In fact, it can be slowed down by parallel write access

First, you must measure whether your media (hard disk) can store images in less than 33 milliseconds – because every 33.333 milliseconds you will get a new image from the webcam

If not, you can't expect this to work

You should (and / or)

>Use more hard disks (e.g. one per thread) > use more cache (e.g. cache controller) > use faster hard disks (e.g. SSDs) > use smaller images (lower resolution) > play some images

If you need to be fast, don't waste time

if elElement<>NIL then
  begin
     JpegImg.Assign(elElement.bmWebcam) ;
     JpegImg.SaveToFile('Save\' + elElement.sTime + '.jpg' );
     elElement.Free;
  end
else
  Sleep(20);

OTL doesn't help make your media faster, but it will be cleaner: O)

So you should see

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>