jeudi 6 décembre 2018

C++ lock a code excution for a scoped block

I need to implement a lock on a scoped block. I found std::lock_guard implementing the same:

std::lock_guard

States that

attempts to take ownership of the mutex it is given. When control leaves the scope in which the lock_guard object was created, the lock_guard is destructed and the mutex is released

But again it states here

If lock is called by a thread that already owns the mutex, the behavior is undefined: for example, the program may deadlock.

std::mutex::lock

Does the std::mutex::lock is called internally by std::lock_guard - I have a scenario that a same callback will callback a member method when a data arrives on a port?

Aucun commentaire:

Enregistrer un commentaire