Thread safe way to save/load data to/from disk in async task?

I’m currently writing a class that’s responsible for saving and loading the server state to/from save files on disk. Due to the nature of my game, it’s possible that some of these save files could be hundreds of megabytes in size (depending on how the world has been created/altered by players).

Right now I’m using FFileHelper to save and load the binary data from disk. Are FFileHelper’s functions thread safe? I want to run the save and load tasks inside of an async task so that if it takes a few seconds to read or write files it won’t freeze the server. This is important since I plan to auto save the server’s state several times per hour.

If FFileHelper is not thread safe, could you please recommend another way to read and write data to disk in a thread safe manner? I’m fine with implementing anything at all in C++.

Before you mention it: Yes my game really does require saving potentially hundreds of megabytes of data to disk to take a snapshot of the server state (the design trades RAM/disk space for computational speed).

Thank you for any suggestions.

1 Like

I will leave some notes about my findings here in case anyone else tries to solve this problem.

So far, running FFileHelper functions in an Async Task to save data to disk is working great. I haven’t run into any race conditions or experienced any crashes. I am passing the Async Task a reference to a bool that it sets true when beginning work and sets false when done, which is checked in the calling function to prevent it from running the save to disk task again before the first task finishes.

You can learn more about how to use an Async Task here:

Will post another update if I run into problems with this method and/or come up with something better.

To write asynchronous, this took care of everything for me: FOutputDeviceFile | Unreal Engine Documentation

Had to copy paste the code to into my own module to make it work in Shipping packaging config without recompiling Engine.

1 Like