Ask coding questions

← Back to all posts
Thread safe vector not working
h
DynamicSquid

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

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 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:

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!

Answered by CSharpIsGud [earned 5 cycles]
View Answer
Voters
DynamicSquid
Comments
hotnewtop
CSharpIsGud

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.

DynamicSquid

@CSharpIsGud Nice thanks! I knew what mutex does but I didn't know where to put it. It works great now, thanks!

xxpertHacker

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)

DynamicSquid

@xxpertHacker huh, weird. you should be getting the ping but maybe repl.it's slow sometimes...

xxpertHacker

@DynamicSquid Na, I only found that you had even posted this because I was looking at CSharp's account.

Coder100 asked this month (think last week), but I chanced upon it almost a week later and never had gotten the ping.

DynamicSquid

@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.

xxpertHacker

@DynamicSquid Ha, yup.

Highwayman

Well... I'm a month late.

Oof I got you beat by like year.
yey my one accomplishment in life lol.

@xxpertHacker

DynamicSquid

@Highwayman lol hi there

Highwayman

XP hi! just randomly surfing old posts.. We don't get enough C/C++ questions on Replit :(
@DynamicSquid

xxpertHacker

@Highwayman Ha, I'd already stopped using Repl.it months ago...

Highwayman

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.

@xxpertHacker

CSharpIsGud

I was able to fix it with this
https://repl.it/@CSharpIsGud/thread-safety