How to notify the main game thread when the FRunnable is done?

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;
}

I used the following website to learn about multithreading and it has an example of the thread calling an event when it is finished:

http://deliciouscookieproduction.com/multi-threading/

Summary:

  • In the FRunnable subclass, the DECLARE_EVENT macro was used to create a type FThreadFinishEvent, which was used to create a variable ResultEvent.
  • When the thread’s Run method was finished, the ResultEvent.Broadcast() method was called
  • In the class that uses an instance of the FRunnable subclass, ResultEvent.AddUObject() method was called, which specified a function to invoke when the event is broadcast…

Hi! Thanks for your answer.
Sorry for the late answer, i have been quite busy lately.

Unfortunately your link is broken :frowning:
I will research FThreadFinishEvent though, which should put me on the way. Thx :slight_smile:

This is wrong. Broadcast will always run bound objects within the same thread. UE4 events/delegates, as far as I can see, cannot be used for inter-thread communication.

1 Like

We can use:
AsyncTask(ENamedThreads::GameThread, Delegate {
Delegate.ExecuteIfBound();
});
It will execute the function in the GameThread.