#include <iostream>
using namespace std;
struct A
{
unsigned int value = 0;
};
A get_set(const A* user_a = nullptr)
{
static A a = { 31 };
// This need to be thread safe
// Multiple readers, one writer
if(user_a)
{
// Copy
a = *user_a;
}
return a;
}
A default_a() {
return get_set();
}
A switch_a(const A& user_a) {
A old_a = get_set();
get_set(&user_a);
return old_a;
}
void set_a(const A& user_a) {
get_set(&user_a);
}
int main() {
{
A def_a = default_a();
cout << def_a.value << endl;
}
{
A my_a = { 32 };
A old_a = switch_a(my_a);
cout << old_a.value << endl;
A def_a = default_a();
cout << def_a.value << endl;
}
{
A my_a = { 33 };
set_a(my_a);
A def_a = default_a();
cout << def_a.value << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IEEgCnsKCXVuc2lnbmVkIGludCB2YWx1ZSA9IDA7Cn07CgpBIGdldF9zZXQoY29uc3QgQSogdXNlcl9hID0gbnVsbHB0cikKewoJc3RhdGljIEEgYSA9IHsgMzEgfTsKCS8vIFRoaXMgbmVlZCB0byBiZSB0aHJlYWQgc2FmZQoJLy8gTXVsdGlwbGUgcmVhZGVycywgb25lIHdyaXRlcgoJaWYodXNlcl9hKQoJewoJCS8vIENvcHkKCQlhID0gKnVzZXJfYTsKCX0KCXJldHVybiBhOwp9CgpBIGRlZmF1bHRfYSgpIHsKCXJldHVybiBnZXRfc2V0KCk7Cn0KCgpBIHN3aXRjaF9hKGNvbnN0IEEmIHVzZXJfYSkgewoJQSBvbGRfYSA9IGdldF9zZXQoKTsKCWdldF9zZXQoJnVzZXJfYSk7CglyZXR1cm4gb2xkX2E7Cn0KCnZvaWQgc2V0X2EoY29uc3QgQSYgdXNlcl9hKSB7CglnZXRfc2V0KCZ1c2VyX2EpOwp9CgoKaW50IG1haW4oKSB7Cgl7CgkJQSBkZWZfYSA9IGRlZmF1bHRfYSgpOwoJCWNvdXQgPDwgZGVmX2EudmFsdWUgPDwgZW5kbDsKCX0KCQ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