Hi all !
I am making progress with multi-threading in ue4 but I am still a bit puzzled.
How can I notify the main thread when my computation in the FRunnable is done so that I can they query the data from the thread (with a thread safe method ofc) ? I have read I can’t fire traditionnal events so I am a bit lost.
I could of course every tick that the thread is done running, but this is not really optimized and I not even sure this wouldn’t freeze everything since I am using a FCriticalSection lock when computing to perform a thread safe operation on FVectors (as suggested here A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums ).
Any idea ?
Here is a snippet of my code :
uint32 ForceFieldWorker::Run()
{
//Initial wait before starting
FPlatformProcess::Sleep(0.03);
//While not told to stop this thread
// and not yet computing the position of atoms
while (!m_Kill)
{
if (m_Pause)
{
//FEvent->Wait(); will "sleep" the thread until it will get a signal "Trigger()"
m_semaphore->Wait();
if (m_Kill)
{
return 0;
}
}
else
{
PC->ClientMessage("Starting Step " + FString::FromInt(counter + 1));
//critical section :
m_mutex.Lock();
/** Compute one step of Force Field computation */
forceFieldStep();
m_mutex.Unlock();
/* increment step counter */
counter++;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//prevent thread from using too many resources
//FPlatformProcess::Sleep(0.01);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (IsFinished()) //if we are done, pause the thread
{
//////////////////////////////////////////////////////////////////
NOTIFY MAIN THREAD HERE
////////////////////////////////////////////////////////////////
m_Pause = true;
}
}
}
return 0;
}