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.
This is how I'm using it:
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
vec2 have different sizes.
I'm thinking of switching to using
char** so I can use
I already tried
std::mutex but that didn't work:
Same result with
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?
Any help would be greatly appreciated!
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.
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)
@xxpertHacker Oh I think I saw his comment you were talking about:
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.
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..
ah yes, many ramblings. I’m so interesting.