How would one go about designing a C++ program that may create over 100 million atomic structs? I would like to ensure that each of this atomic struct won’t be written by two different threads. Each of these struct needs to persist throughout the program (since they may be modified in the future), but most likely they will only be used for a second.
In the cppreference atomic_flag documentation, one should not be using spin-locks.
Yet in a linux program, I don’t think it is possible for the operating system to manage that many mutexes at once, despite pthread_mutex_init indicating that there is no limit.
Questions:
- Is it a good idea to have an atomic_flag in each of the struct, and have an RAII wrapper that grabs the atomic_flag?
- Is there any alternative solution that I should consider (eg using a mutex instead of an atomic_flag)?
My existing code:
struct S { int a; int b; .... int z; }; // assume there is 26 int fields
std::vector<S> v; // may contain > 100m objects
S getObject(size_t index) { return v[index]; }
int getSum(S s) { return a + b + ... + z; } // eg of a function i need
int update(S s, T t) {s.k = t.something; s.p = t.something_else;} // there can be multiple writers, upon each update, set a subset of fields in S to what another struct T contains
Some notes:
- assume that upon every update, as long as the update was done atomically, the object will be in a valid state.
- let me know if there are any other information that I should provide, but did not.
- I’m still in the designing phase, so I don’t have any reproduce-able code.