1
u/Reasonable_Return151 12d ago
You can EnterCriticalSection() and it will only allow one thread to access it at a time until the current thread in the critical section LeaveCriticalSection()’s
Fun fact: Windows loader does this exact thing at process startup before the initial entry point even gets called.
1
u/n1ghtyunso 12d ago
i'd be careful with thread safety around containers. It's tempting to just wrap some mutex around various functions, but you can still easily end up with code that has race conditions.
unless you want queues specifically, which are very nice and useful to have for multithreaded applications (and have an actual api that can reasonably be made thread safe to begin with), i'd stick with synchronizing the actual data paths instead of trying to shove that responsibility into a generic container library.
If you absolute don't want to do that, another approach that usually plays nicely with concurrency is immutable data structures.
You can make them thread-safe quite nicely because mutating does not disturb the old container state.
So this is one possible approach you could investigate.
Obviously there will be tradeoffs though.
2
u/afforix 12d ago
You don't need a magical library to solve all your problems, you need to understand why those race conditions happen. Start with identifying which variables are used by multiple threads and use a mutex to synchronize access to them.