Thread safe vector not working
I have three std::string
vectors. 2 of those vectors contain different strings, and the third vector is just to "point" to one of those vectors.
For example
const std::vector<std::string> vec1{ "dynamic", "squid", "s" } const std::vector<std::string> vec2{ "static", "squid" }; std::vector<std::string> vec = vec1;
This is how I'm using it:
void thread1() { for (int a = 0; a < vec.size(); ++a) std::cout << vec[a] << '\n'; } void thread2() { if (switchSquidType) { if (vec == vec1) vec = vec2; else vec = vec1; } }
So there are two threads, one to display the information, and one to modify it.
However I found that when I'm drawing vec
, it sometimes switches to the other vector mid-draw! That results in an array out of bounds error since vec1
and vec2
have different sizes.
I'm thinking of switching to using char**
so I can use std::atomic
.
I already tried std::mutex
but that didn't work:
std::mutex m; void thread1() { m.lock(); for (int a = 0; a < vec.size(); ++a) std::cout << vec[a] << '\n'; m.unlock(); } void thread2() { if (switchSquidType) { if (vec == vec1) vec = vec2; else vec = vec1; } }
Same result with std::lock_gaurd
.
I linked the repl so you can see what I'm talking about. If you hold down ENTER, you'll see the problem.
So how should I approach this problem? And I doing something wrong with std::mutex
? Should I switch to std::atomic<char**>
? Or should I completely redesign my project?
I summon the C/C++ people to aid me with my quest (please?):
@xxpertHacker
@highwayman
@MocaCDeveloper
Any help would be greatly appreciated!
Well... I'm a month late.
Never got the ping, I think when you ping people in a post, or maybe if you ping multiple people at once, they won't get the ping, at least I haven't.
For anything thread related, CSharpIsGud is who you're looking for.
(you're not the only one who had ever pinged me for a C++ question, so it's annoying that I haven't been getting the pings)
Coder100 asked this month (think last week), but I chanced upon it almost a week later and never had gotten the ping.
People like Coder100 don't get their upvotes from answering questions like https://repl.it/talk/ask/Thread-safe-vector-not-working/58685 which really need to be answered.
Well... I'm a month late.
Oof I got you beat by like year.
yey my one accomplishment in life lol.
XP hi! just randomly surfing old posts.. We don't get enough C/C++ questions on Replit :(
@DynamicSquid
Ha, I still have you beat! XP
Me too- my activity in the forums dropped abysmally low like at least a year ago.. :(
I’m kinda sad about it.
It would be nice if there were more mainly C++ programmers with an active interest in the community- I really like C++ questions… kinda selfish of me but whatever..
@xxpertHackerah yes, many ramblings. I’m so interesting.
I was able to fix it with this
https://repl.it/@CSharpIsGud/thread-safety
Basically, from what I read of mutexes here is what happens when you use one.
After you call lock() no other thread can execute the code up to unlock() until unlock() is reached.
Therefor if you just lock and unlock from within thread individually it has no effect because the other threads don't ever enter the same place in the program.
But if you make a function that they both have to call to acquire the lock then they have to take turns and can't access it at the same time.