UE4 | C++ | 资源加载时候的线程选择与使用
加载时间长的资源采用异步方式,(注意加线程锁和临界区) 加载时间短的资源采用同步方式。(注意打Log评估卡顿)
1. 异步加载可以使用Async 配合TaskGraph 快速实现
项目中song部分的ai计算和初始化比较耗时间,所以采用异步加载方式
void ALgoController::Prepare_song(FString songid)
{
dele_blankFrame.Broadcast(0.f, 0.f, 0.f, 0.f);
if (ALgoController::seq_song != nullptr) {
lgoStopSeq(seq_song);
lgoFreeSeq(lgoctx, seq_song);
LEDClientInstance->LEDSeqSong = 0;
}
if (islgoLoaded) {
Async(EAsyncExecution::TaskGraph,
[this, songid] {
UE_LOG(LgoInitLog, Warning, TEXT("====Load Song : Start===\r\nLoad Song : StartTime:%f"), FDateTime::Now().GetTimeOfDay().GetTotalSeconds());
MakeSeq_song(songid);
UE_LOG(LgoInitLog, Warning, TEXT("====Load Song : End===\r\nLoad Song : EndTime:%f"), FDateTime::Now().GetTimeOfDay().GetTotalSeconds());
GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Green, TEXT("Song is ready to play"));
curSongID = songid;
isSongPrepareFinished = true;
},
[this](){
if (lgoState == LGO_STATE::SeqReady) {
lgoState = LGO_STATE::CanPlay;
};
});
}
else {
UE_LOG(LgoInitLog, Warning, TEXT("====Prepare_song : Please load lgo first ===="));
dele_PreSongFinished.Broadcast(false, songid,0);
}
}
1.5 加临界区和线程锁的方式
- 使用
FCriticalSection 加临界区 - 使用
FScopLock 加线程锁 - 使用
delete 手动解锁,或者等待下一个} 自动解锁
static FCriticalSection readSeqSection;
FScopeLock* readSeqLock = new FScopeLock(&readSeqSection);
...
要执行的部分();
...
delete readSeqLock;
2. 同步加载注意加Log 来评估耗时
项目中eff部分的ai计算和初始化很快,所以采用同步加载方式
void ALgoController::Prepare_effect(FString effId)
{
dele_blankFrame.Broadcast(0.f, 0.f, 0.f, 0.f);
UE_LOG(LgoInitLog, Warning, TEXT("====Load Eff : Start===\r\nLoad Song : StartTime:%f"), FDateTime::Now().GetTimeOfDay().GetTotalSeconds());
MakeSeq_eff(effId);
UE_LOG(LgoInitLog, Warning, TEXT("====Load Eff : End===\r\nLoad Song : EndTime:%f"), FDateTime::Now().GetTimeOfDay().GetTotalSeconds());
GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Green, TEXT("Eff is ready to play"));
curEffID = effId;
isEffPrepareFinished = true;
if (lgoState == LGO_STATE::SeqReady) {
lgoState = LGO_STATE::CanPlay;
}
}
|