djchase
(djchase)
February 20, 2020, 1:38pm
4
anonymous_user_105a792b:
Hi djchase,
Thank you for your kind & helpful advice!
Yes, that’s exactly what I’m looking for. I’ll try to use FScopeLock in accessor-functions so that reader/writer should get the lock before entering their critical section.
BTW, I’m writing the UDP receiver in MyGameInstance.cpp as follows, refering to Rama’s UDP receiver .
UDPReceiver = new FUdpSocketReceiver(ListenSocket, ThreadWaitTime, TEXT("UDP RECEIVER"));
UDPReceiver->OnDataReceived().BindUObject(this, &ARamaUDPReceiver::Recv);
ARamaUDPReceiver::Recv plays the role of a shared resource writer in my case. Since it is designated as a call back function, it is executed whenever network packets arrive.
On the other hand, a corresponding shared resource reader is in a Blueprint script (BP_PlayerPawn which inherits Pawn class) and it runs at Tick event timing.
My understanding is that these two code, ARamaUDPReceiver::Recv in MyGameInstance.cpp and Tick event in BP_PlayerPawn, are running on different threads.
That’s why I need to protect the critical section. Is my understanding correct?
Many thanks
Hi .
You are welcome :).
Yep, that sounds 100% correct.
The UDPReceiver and the game-ticks run on different threads. Maybe it would be already sufficient to ensure that ARamaUDPReceiver::Recv and BP_PlayerPawn::Tick can never run at the same time.
But it depends when and where the data is accessed. If it can happen, that you access those shared resources outside above two functions, you may want to make the accessor-functions safe as well.
Good luck!