Can scoped_lock lock a shared_mutex in read mode?

Can scoped_lock lock a shared_mutex in read mode?

C++17 introduced both&nbsp;<code>std::shared_mutex</code>&nbsp;and&nbsp;<code>std::scoped_lock</code>. My problem is now, that it seems, that&nbsp;<code>scoped_lock</code>&nbsp;will lock a shared mutex always in exclusive (writer) mode, when it is passed as an argument, and not in shared (reader) mode. In my app, I need to update an object&nbsp;<code>dst</code>with data from an object&nbsp;<code>src</code>. I want to lock&nbsp;<code>src</code>&nbsp;shared and&nbsp;<code>dst</code>&nbsp;exclusive. Unfortunately, this has the potential for deadlock, if a call to another update method with&nbsp;<code>src</code>&nbsp;and&nbsp;<code>dst</code>&nbsp;switched occurs at the same time. So I would like to use the fancy deadlock avoidance mechanisms of&nbsp;<code>std::scoped_lock</code>.

C++17 introduced both std::shared_mutex and std::scoped_lock. My problem is now, that it seems, that scoped_lock will lock a shared mutex always in exclusive (writer) mode, when it is passed as an argument, and not in shared (reader) mode. In my app, I need to update an object dstwith data from an object src. I want to lock src shared and dst exclusive. Unfortunately, this has the potential for deadlock, if a call to another update method with src and dst switched occurs at the same time. So I would like to use the fancy deadlock avoidance mechanisms of std::scoped_lock.

I could use scoped_lock to lock both src and dst in exclusive mode, but that unnecessarily strict lock has performance backdraws elsewhere. However, it seems, that it is possible to wrap src's shared_mutex into a std::shared_lock and use that with the scoped_lock: When the scoped_lockduring its locking action calls try_lock() on the shared_lock, the later will actually call try_shared_lock() on src's shared_mutex, and that's what I need.

So my code looks as simple as this:

struct data {
    mutable std::shared_mutex mutex;
    // actual data follows
};

void update(const data& src, data& dst) { std::shared_lock slock(src.mutex, std::defer_lock); std::scoped_lock lockall(slock, dst.mutex); // now can safely update dst with src??? }

Is it safe to use a (shared) lock guard like this inside another (deadlock avoidance) lock guard?

c++

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.