Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I use a lock if I am sure that every thread will always write the same value to shared memory?

C++ : I have a vector<bool> , no thread will ever write false to any of the indices . Should I still use a lock ? I don't think it will cause a race condition as all threads are writing the same value . The functions being accessed by multiple threads looks like :

void setVal(int index) 
{
  if (boolvec[index] == false)
    boolvec[index] = true; 
}
like image 202
spooja__ Avatar asked Nov 27 '25 10:11

spooja__


1 Answers

To get by without a lock you should use a std::vector<atomic_flag_wrapper> where atomic_flag_wrapper wraps a std::atomic_flag similar to the code in this answer.

With std::vector<bool> you must use a lock, the standard explicitly tells you so:

Notwithstanding [res.on.data.races], implementations are required to avoid data races when the contents of the contained object in different elements in the same container, excepting vector<bool>, are modified concurrently.

http://eel.is/c++draft/container.requirements.dataraces#2 (C++ draft, 02.08.2020), emphasis mine

In plain English:

std::vector<bool> does not need to make sure that writes to two different elements are race-free; therefore there can be a data race; therefore you need a lock.

If it were e.g. a std::vector<char>, then the standard makes sure that charVector[0] and charVector[1] can be written to concurrently. But still then you cannot write to charVector[0] from more than one thread concurrently; you need to use atomics.

std:atomic<bool> is not guaranteed to be lock-free, so you should use std::atomic_flag which has this guarantee. You cannot put these into a std::vector, though, because they are not copy constructible. For that, you need a wrapper as described in this answer.

like image 135
Daniel Jour Avatar answered Dec 02 '25 06:12

Daniel Jour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!