Thread safety best practices

Hi guys,
I have a class that uses a thread to update a local file in the background. This file is read by various plugins and, obviously, plugins are randomly crashing when more there is more than one instance trying to read/write that file. I’m pretty new with threading and I’m wondering what are the best practices to avoid conflicts between more instances of the same thread.


Sounds like a case for an InterProcessLock…

Thanks. I also just found ReadWriteLock that should do what I’m looking for.

ReadWriteLock doesn’t refer to reading or writing a file, it is about how to prioritize access. It will allow multiple parallel read access, but only exclusive write access.

You will need a CriticalSection, that is accessible from all possible instances. InterProcessLock is working in all hosts, even those with separate processes for each instance (AUv3 and some other hosts as opt-in).

An easier solution would be to have the file access code in a SharedResourcePointer, that can handle the locking in memory.

And there is the PropertiesFile class, that seems to be already optimised for access from different instances, so it might have built in what you need.

ah good to know. thanks daniel!

Looks like you need to provide an InterProcessLock for that use case though, doesn’t seem to be thread safe ootb.

Ah, thanks, it is in PropertiesFile::Options.

I remembered to read in the docs the comment that it handles that situation, but seems not by default. Thought it was in ApplicationProperties, but can’t find it, so probably my memory was wrong.

You mean « corrupted » :grin: