Probably best to make that an atomic bool too… ![]()
Is there a platform, where bool isn’t atomic? 
but for consistency, probably yes…
Yes, it’s complicated…The bools may be atomic at the hardware level, but the compiler might do weird stuff with the variables if they are not explicitly handled as atomic in the code. (Atomic ensures the compiler isn’t going try anything “smart” with the variable.)
So sorry for my coding noobiness but… on the bool flag - is the idea to check for errors in my functions to check in case something returns null, such as:
bool flag;
if (auto* wavWriter = format.createWriterFor (new FileOutputStream (file)) == nullptr
flag == false;
And then in the process block have some kind of a…
while (flag == true)
Is this roughly what you meant? Thanks again
EDIT: Or should I maybe make the whole function boolean, and if something goes wrong I return false?
The idea is, if opening the file is asynchronous, you have to make sure you don’t try to write while in the process of opening. I think you don’t have to worry about it, if you do that in prepareToPlay and implement that synchronously, since the processing can only start, once all prepareToPlay calls have finished. But the cleaner design would be to have such a flag, that indicates, if you are ready to record, and if you want record at all…
That is, because the processBlock must not be held back by anything.
Seeing my answer of >4 years ago I am embarrassed. I learned some stuff in the meantime:
It doesn’t matter, if it is an atomic operation/atomic type.
ALWAYS wrap into atomic, if it can be accessed from different threads. This is to stop the optimiser from making false assumptions. The optimiser can break your code!
See Fabian and Dave’s Realtime 101 on youtube and, if you have access to this years ADC, Fabians Realtime Confessions!
(Sorry Xenakios, thanks for correctng me back then)
I think asking that question is practically a right of passage to writing C++!
Yes, and check it is lock free too with is_lock_free() (which is usually the case in PC’s and Macs). If you want to be totally certain, for a bool you have atomic_flag and for an int, in c++20 you can use atomic_signed_lock_free and atomic_unsigned_lock_free (to be honest, I haven’t tried those yet though).
https://en.cppreference.com/w/cpp/atomic/atomic
You can also quickly check some pre-defined macros.