0

I am writing a C++ program and am currently having an issue where I need atomics, preferably std::atomic_flag, to have thread safe flags. The issue is that I need multiple different atomic flags and rather than declaring them manually like this:

std::atomic_flag flag1 = ATOMIC_FLAG_INIT;
std::atomic_flag flag2 = ATOMIC_FLAG_INIT;
std::atomic_flag flag3 = ATOMIC_FLAG_INIT;

I want to ideally declare them in an array (which I read online is not possible). The reason I want to store them in a container is that I want to access them using indexes. My different threads are each given an index which tells them which of the atomic_flags is assigned to them. Within the main thread, I can use those indexes to set the flags accordingly.

[EDIT] I have successfully identified all my errors. I kept getting access violation errors and have wrongfully assumed them to be related to the atomic variables stores in an array. It was actually a mistake at how I passed my index to the thread.

Custos
  • 68
  • 6
  • 2
    Can you explain exactly why you believe you need atomic objects, and why exactly you believe you cannot have arrays of them? Don't believe everything you "read online". Any clown can put together any web site that says anything. As far as learning the fundamentals of the most complicated general purpose programming language in use today, the only way to do it effectively is [from a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and not to "read online", or watch Youtube videos. – Sam Varshavchik Nov 04 '20 at 03:04
  • @SamVarshavchik As to the reason why I need atomic objects is that I need a thread safe way to read a boolean flag inside of my thread that is set from within the main thread. And as to why I said that I cannot have atomics inside of an array, that is because I tried that first and when I got lots of errors, I tried fixing them and found out from a lot of different sources that you cannot have an array of atomics. – Custos Nov 04 '20 at 03:11
  • 3
    `std::atomic_flag flags[2]={ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT};` compiles just fine for me. You are mistaken that you cannot have an array of atomics. If you can declare a single object, C++ gives you a 100% iron-clad guarantee that you can have an array of them. Always. As far as thread safety goes, there's more to thread safety than atomics. There's also full synchronization, while atomics don't provide complete thread-safe synchronization between threads. This is why reading random coding blogs or watching Youtube videos is not sufficient, a good C++ textbook is pretty much a requirement. – Sam Varshavchik Nov 04 '20 at 03:27
  • @SamVarshavchik Thank you very much, this works in my code as well. I am not sure what I did wrong... – Custos Nov 04 '20 at 03:40
  • 2
    @Custos When you have errors, you should read the error message. That often leads to a solution. – eerorika Nov 04 '20 at 03:59
  • 1
    Perhaps you tried to make a `std::vector`? That's generally not usable because many `vector` function can reallocate the storage and copy-construct into new space, but atomic objects of course delete their copy constructors to prevent this kind of mistake. `std::array<>` might work. – Peter Cordes Nov 04 '20 at 04:44
  • FYI: [How to update two atomic counters at once](https://stackoverflow.com/a/44692164/7478597) ;-) – Scheff's Cat Nov 04 '20 at 06:55

0 Answers0