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
Sleepthis way and expect decent performance. You need real synchronization.