Skip to content
← Back to Community
Thread safe vector not working
Profile icon
hacker
has Hacker Plan
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

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!

Answered by CSharpIsGud [earned 5 cycles]
View Answer
Voters
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
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.

Profile icon
DynamicSquid

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

Profile icon
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)

Profile icon
DynamicSquid

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

Profile icon
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.

Profile icon
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.

Profile icon
xxpertHacker

@DynamicSquid
Ha, yup.

Profile icon
Highwayman

Well... I'm a month late.

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

@xxpertHacker

Profile icon
DynamicSquid

@Highwayman
lol hi there

Profile icon
Highwayman

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


@DynamicSquid

Profile icon
xxpertHacker

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

Profile icon
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

Profile icon
CSharpIsGud

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