Questions tagged [lock-guard]
15 questions
6
votes
3 answers
Troubles with std::lock_guard and if constexpr block
There is a class template Foo. And for some specific type, a function should use lock_guard.
Here is the example code:
#include
#include
#include
template
class Foo {
public:
void do_something(int…

Wayne Tseng
- 137
- 5
6
votes
1 answer
Should I use lock_guard, scoped_lock or unique_lock in this situation?
I have read many of the questions already answered that relate to this but none of them gave me a clear understanding of which I should be using when I have multiple writers but a single reader. The code below is a contrived example of what I'm…

poby
- 1,572
- 15
- 39
4
votes
2 answers
mutex used in a function defined inside a class doesn't seem to work when that function is called in a thread in main
#include
#include
#include
class ThreadLessons {
private:
std::mutex _threading_mutex_in_class;
public:
ThreadLessons() {}
ThreadLessons(const ThreadLessons &tl) {}
ThreadLessons operator=(const…

robot9
- 43
- 4
4
votes
1 answer
Can using the lock of a mutex, adopted by a lock_guard, lead to a UB?
Can the following snippet cause an undefiled behaviora due to using the lock of the mutex already adopted by a lock_guard? and will it be safe if I use unique_lock instead of lock_guard in the same snippet? I know that there are…

asmmo
- 6,922
- 1
- 11
- 25
3
votes
2 answers
Understanding cppreference example on lock
While reading on c++ std::lock, I ran into the following example from the cppreference:
void assign_lunch_partner(Employee &e1, Employee &e2)
{
static std::mutex io_mutex;
{
std::lock_guard lk(io_mutex);
std::cout…

Iron Fist
- 10,739
- 2
- 18
- 34
1
vote
2 answers
c++11: thread with mutex sees atomic variable's value changing despite this being the only code that can change it
An atomic variable (128-bit structure in this case) is being updated, to the surprise of the only thread that would have the ability to update it. How so?
This is a minimal example so it doesn't do anything that makes sense, but: an alloc()…

Swiss Frank
- 1,985
- 15
- 33
0
votes
1 answer
Replacement for `std::bind` with Visual Studio 2019?
I've got code that compiles with Visual Studio 2017 that uses std::bind:
std::unique_lock m_lock(m_mutex_wait_for_message);
m_cond_variable.wait(m_lock, std::bind(&Logging::is_message_available, this));
std::lock_guard …

Thomas Matthews
- 56,849
- 17
- 98
- 154
0
votes
1 answer
What exactlly is lock with adopt_lock_t
When I checked adopt_lock_t,
it says adopt_lock_t assume the calling thread already has ownership of the mutex.
So what's the meaning of the word assume? What if other thread already holding the mutex when I claim the lock(adopt_lock_t) with the…

f1msch
- 509
- 2
- 12
0
votes
3 answers
Why doesn't mutex work without lock guard?
I have the following code:
#include
#include
#include
#include
int shared_var {0};
std::mutex shared_mutex;
void task_1()
{
while (true)
{
shared_mutex.lock();
const auto temp =…

SNR_BT
- 133
- 5
0
votes
1 answer
scoped_lock inside lock_guard, is it redundant?
I'm new in multi-thread programming.
I'm now doing a serial port communication project, and search related codes for reference.
I found a code that someone used scoped_lock inside lock_guard as below:
void B(){
boost::mutex::scoped_lock…

RyanChen.YLC
- 57
- 7
0
votes
0 answers
Scoped lock_guard Not Releasing Mutex in Static Method
Platform Debian 9 | ARM
I have a class that is used to store thread IDs on a std::list and then print the list as part of the shutdown procedure. There are two static methods: AddId() and PrintIds(). Since the static methods are used by several…

Jerry
- 41
- 5
0
votes
1 answer
How to pass only mutex to lock_guard constructor parameter
When declare locker like,
lock_guard Locker(mLocker);
I want the compiler to detect if an mLocker is a mutex.
To implement this, I used concept requires and defined as below.
template
concept is_mutex = requires
{
std::is_same_v

정명준
- 5
- 3
0
votes
0 answers
problem while accessing same object from multiple threads simultaneously using lock_guard
Peer* Subtracker::getPeer(int packetNum, Peer* p, Peer* nearestP)
{
lock_guard guard(this->m);
double d = DBL_MAX;
for(auto it:packetToPeersMapping[packetNum])
{
double dTemp = sqrt(pow(p->x - it->x, 2) + pow(p->y -…

Priyanshu
- 21
- 1
- 6
0
votes
0 answers
Returning a reference from under a lock: Idiomatic or just too darn clever?
Suppose you have a data structure where values are kept in nodes, e.g., the standard maps or lists, and that structure has a lock to guard it. Suppose also the value types (mapped_type for the maps) is complex enough that each one has its own…

davidbak
- 5,775
- 3
- 34
- 50
-1
votes
1 answer
C++ : Is calling a method of another class using pointer in a thread safe?
I am calling a member function inside the thread. I have a member variable that I edit inside this function for which I have applied the lock.
Is this logic okay or do even the reads need to be thread-safe?
The code is something like the one shown…

user2456835
- 11
- 1