gpt4 book ai didi

multithreading - 替换为Thread.Suspend-恢复而不影响性能

转载 作者:行者123 更新时间:2023-12-03 13:16:36 26 4
gpt4 key购买 nike

我正在制作具有过渡-缩放-平移-效果的幻灯片,其性能对于平滑渲染至关重要。这是一个我要移植到Delphi 10.3的旧项目,有一个线程可以在运行渲染到屏幕的同时从jpgeg-images的文件流中加载下一张幻灯片。在我的旧代码中,线程一直在运行,被挂起并根据需要恢复。这是现在不推荐使用的旧代码的流程:

procedure TSlideshow.Create;
begin
MakeNextThread:=TMakenextThread.Create(true);
MakeNextThread.FreeOnTerminate:=false;
end;

procedure TSlideshow.Render(previous, next: integer);
begin
//Check if thread has loaded next, if not make it here

//Send off the thread to make next+1
//The next block takes less than 0.1 ms on average !!!
If next+1 < count then
begin
//Fill fields of MakeNextThread with data to load next+1
MakeNextThread.Resume;
end;

//Render to screen
end;

procedure TMakeNextThread.Execute;
begin
while not terminated do
begin
//Load bitmap from jpeg-stream
sleep(1)
//Prepare alpha-channel for transition
done:=true;
suspend;
end;
end;

我试图避免使用暂停和恢复,但是结果是 Not Acceptable ,因为它会导致幻灯片在过渡之前明显暂停。
procedure TSlideshow.Render(previous, next: integer);
begin
//Check if thread has loaded next, if not make it here

//Send off the thread to make next+1
//The next block takes more than 40 ms on average !!!
If next+1 < count then
begin
MakeNextThread.terminate;
MakeNextThread.free;
MakeNextThread:=TMakeNextThread.Create(true);
MakeNextThread.FreeOnTerminate:=false;
//Fill fields of MakeNextThread with data to load next+1
MakeNextThread.Start;
end;

//Render to screen
end;

procedure TMakeNextThread.Execute;
begin
//Load bitmap from jpeg-stream
sleep(1)
//Prepare alpha-channel for transition
done:=true;
end;

有没有办法在不损失性能的情况下做到这一点?首先使用暂停恢复有什么危险呢?

最佳答案

您不需要每次都创建一个新线程。只需在TEvent内使用TMakeNextThread对象即可。让Execute()运行一个调用TEvent.WaitFor()的循环,然后您可以在想要“唤醒”线程时调用TEvent.SetEvent(),而在想要“暂停”线程时可以调用TEvent.ResetEvent()。当事件被信号通知且Terminated为False时,循环即可完成其工作。

关于multithreading - 替换为Thread.Suspend-恢复而不影响性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62365665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com