220 26827 <2DCF8C2C-C662-49EE-B7E5-3FA11B47AB58@gmail.com> article
Path: news.gmane.org!not-for-mail
From: Howard Hinnant <howard.hinnant@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: std::lock and std::lock_guard for
 SharedLockable's lock_shared
Date: Tue, 12 Jul 2016 15:34:28 -0400
Lines: 115
Approved: news@gmane.org
Message-ID: <2DCF8C2C-C662-49EE-B7E5-3FA11B47AB58@gmail.com>
References: <8d4cc002-f0f2-465f-b856-841adc1ecffd@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\))
Content-Type: multipart/signed; boundary="Apple-Mail=_C393CB53-614F-4929-9F35-8246084FC829"; protocol="application/pgp-signature"; micalg=pgp-sha256
X-Trace: ger.gmane.org 1468352087 6462 80.91.229.3 (12 Jul 2016 19:34:47 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 12 Jul 2016 19:34:47 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCK2HM4L6YERBTMMSW6AKGQECGCF5TI@isocpp.org Tue Jul 12 21:34:41 2016
Return-path: <std-proposals+bncBCK2HM4L6YERBTMMSW6AKGQECGCF5TI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qk0-f199.google.com ([209.85.220.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCK2HM4L6YERBTMMSW6AKGQECGCF5TI@isocpp.org>)
	id 1bN3S3-0004tb-Bb
	for gclcip-std-proposals@m.gmane.org; Tue, 12 Jul 2016 21:34:39 +0200
Original-Received: by mail-qk0-f199.google.com with SMTP id a123sf54164945qkd.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 12 Jul 2016 12:34:39 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:subject:from:in-reply-to:date:message-id:references:to
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=ibEP027JSYDViRIpKgLYBMR9RYxRGFKkhNsGVeLrslc=;
        b=DupJZIIslq0b7pcgM9Mx5yOGgbzkMlxnrbS63N4lW39KQllbxoRDkrins8Av5yTMVv
         QEe26xHiURaKl0D3+PCVTJs8vSuzLipZ6NDAvjbiQnNCZ1lXsYuNPFhEd1wgsnf6Xzi0
         BN7BSbiqlaieSatXt8L2kKdYVzz8QZl/E0h2fKGxiNdrkJ/hJ+Ygb+PpApYZlMB77ic/
         nEnN+IvohnNYvhux91F25N6Pyme7YtiSGQxvWVpunKMcdRhEJeRa6Dy1lRthAqB2rdKy
         YtVxFwVT6dA+L1RjcE3VnbnWU3LJuT/WxsOMSuQbNNPAmWzGYmZBv3teVtcSl/FJpyro
         JCAg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:subject:from:in-reply-to:date
         :message-id:references:to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=ibEP027JSYDViRIpKgLYBMR9RYxRGFKkhNsGVeLrslc=;
        b=aapzXnnXitFxv5DI2uN85FW0M69kdsA41xeqBqDOHyJOWPkWA7ifGG3I5AfZ78U0rJ
         sWPVD81guZXtgRyG7xOr9uDZ5ShppA4VFT+q0+YHd4SaUbE1c8Kck44sxuLADGr2/mAm
         OgAlkblbqjEr4H5d/JgHAVI1LCTVu8hoQRAMmARRijvNbXtXewYPRgu94s2NCNVon51O
         YIWyryE+UsACAUv25YJ0C5Q3tyRrItkW8IPcc+oOPrYIfHLMTj0MKvRD2DLxSRBdiZ0z
         Wlec1k4lphNt5irF/MfLuJ15oXx3ZqFCT5gVq3oT/6SMgOnEC6rCjlzul0BqfHupP1Wb
         8CeQ==
X-Gm-Message-State: ALyK8tKAttdV99U50eRQRtzUh9eU2tp6IDe3fivP9xrdsX8jEVYmgsLdKE+FR+zXRRQSkQ==
X-Received: by 10.200.46.149 with SMTP id h21mr3141878qta.3.1468352078432;
        Tue, 12 Jul 2016 12:34:38 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.108.213 with SMTP id w204ls451720itb.1.gmail; Tue, 12 Jul
 2016 12:34:37 -0700 (PDT)
X-Received: by 10.55.217.89 with SMTP id u86mr5269743qki.114.1468352077539;
        Tue, 12 Jul 2016 12:34:37 -0700 (PDT)
Original-Received: from mail-qt0-x232.google.com (mail-qt0-x232.google.com. [2607:f8b0:400d:c0d::232])
        by mx.google.com with ESMTPS id c89si3322890qkj.30.2016.07.12.12.34.37
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 12 Jul 2016 12:34:37 -0700 (PDT)
Received-SPF: pass (google.com: domain of howard.hinnant@gmail.com designates 2607:f8b0:400d:c0d::232 as permitted sender) client-ip=2607:f8b0:400d:c0d::232;
Original-Received: by mail-qt0-x232.google.com with SMTP id 52so13908492qtq.3
        for <std-proposals@isocpp.org>; Tue, 12 Jul 2016 12:34:37 -0700 (PDT)
X-Received: by 10.200.46.122 with SMTP id s55mr6209201qta.80.1468352077187;
        Tue, 12 Jul 2016 12:34:37 -0700 (PDT)
Original-Received: from [10.0.1.2] (cpe-67-241-68-255.twcny.res.rr.com. [67.241.68.255])
        by smtp.gmail.com with ESMTPSA id p39sm908710qtp.14.2016.07.12.12.34.35
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
        Tue, 12 Jul 2016 12:34:35 -0700 (PDT)
X-Pgp-Agent: GPGMail
In-Reply-To: <8d4cc002-f0f2-465f-b856-841adc1ecffd@isocpp.org>
X-Mailer: Apple Mail (2.3124)
X-Original-Sender: howard.hinnant@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 howard.hinnant@gmail.com designates 2607:f8b0:400d:c0d::232 as permitted
 sender) smtp.mailfrom=howard.hinnant@gmail.com;       dmarc=pass (p=NONE
 dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:26827
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/26827>


--Apple-Mail=_C393CB53-614F-4929-9F35-8246084FC829
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8

On Jul 12, 2016, at 3:09 PM, 'Bernd' via ISO C++ Standard - Future Proposal=
s <std-proposals@isocpp.org> wrote:
>=20
> In discussions on Andrei's NDC talk about folly/Synchronized [1] the ques=
tion came up why his deadlock prevention [2] did not just use std::lock(). =
It was pointed out that this is due to using SharedLockable::lock_shared(),=
 making std::lock() the wrong thing. There is the workaround of
>=20
>     std::shared_mutex a, b;
>     std::shared_lock<> lock_a (a, std::defer_lock);
>     std::shared_lock<> lock_b (b, std::defer_lock);
>     std::lock (lock_a, lock_b);
>=20
> using the shared_lock adapter exposing SharedLockable as Lockable. Note t=
hat the inverse order of std::lock and std::shared_lock (using std::adopt_l=
ock) requires manual deadlock prevention. Note that the locks also have to =
be non-const which is undesirable.
>=20
> I propose adding std::lock_shared (SharedLockable&...) to allow said inve=
rse order, for consistency:
>=20
>     std::shared_mutex a, b;
>     std::lock_shared (a, b);
>     std::shared_lock<> const lock_a (a, std::adopt_lock);
>     std::shared_lock<> const lock_b (b, std::adopt_lock);
>=20
> On the same note, C++17 added std::lock_guard<Lockable...>, which has no =
SharedLockable variant either, which would make the above code even easier:
>=20
>     std::shared_lock_guard<> const lock_both (a, b);
>=20
> The biggest issue I can see with it is that combining locking a Lockable =
and a SharedLockable at once, which still requires the defer_lock approach =
or manual deadlock prevention. It feels like this was an oversight when add=
ing SharedLockable and an easy addition of basically "do as std::lock, but =
use _shared methods=E2=80=9D.

I can assure you it wasn=E2=80=99t an oversight.  And your first =E2=80=9Cw=
orkaround=E2=80=9D is the intended use.

We don=E2=80=99t need or want a =E2=80=9Ckitchen sink=E2=80=9D API where th=
ere are 3 three different ways to do everything.  We already have that with=
 std::string, and we don=E2=80=99t need to repeat that design mistake.

The =E2=80=9Cmixed case=E2=80=9D you speak of at the end is actually a very=
 prominent use case:

    class A
    {
        using ReadLock  std::shared_lock<std::shared_mutex>;
        using WriteLock std::unique_lock<std::shared_mutex>;

        mutable std::shared_mutex mut_;
    public:
        // ...
        A& operator=3D(const A& a)
        {
            if (this !=3D &a)
            {
                WriteLock this_lock(mut_, std::defer_lock);
                ReadLock  that_lock(a.mut_, std::defer_lock);
                std::lock(this_lock, that_lock);
                // mut_ is now unique-locked and a.mut_ is now share-locked
                // ... now safe to assign data ...
            }   // mut_ and a.mut_ now unlocked, even if there was an excep=
tion
            return *this;
        }
        // ...
    };

Howard

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/2DCF8C2C-C662-49EE-B7E5-3FA11B47AB58%40gmail.com=
..

--Apple-Mail=_C393CB53-614F-4929-9F35-8246084FC829
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP using GPGMail

-----BEGIN PGP SIGNATURE-----

iQIcBAEBCAAGBQJXhUZKAAoJEGbcHCxKqhWCX2YP/0mqL7P/U1Ms2rqDm8Mwkvw+
vWsIHav10+U27M63jz9Az4xb3VBAKlmMGY/BFWozJookgVxH8LbZyWHoBRyOEsS6
C5w+QRJxNo1BF0UcbRXwHIG5pscSovPwlc1Y74mDCom9U+JA7gzJF9JF5keQCmwG
lDUfcNRB+5huygMMk/8WSWNgdgPCCDEJu9NtMDer1fWT5M1bo3qGal/vPNmXySM5
B5LmD4GZBWpPpn2Q/YxYbIRtGQ6vUCDu+NbF6ou59pT6tkczk9GZNo8v+JmMWqPj
7ZdG1Kul7LTZgaNb0oV/p9l31lkGF5Vsz/pHc+KXLIM1L7e70UlnPxbUMPOoDDxV
+gOnnWEsNFzSgztvKVSR2JAve9ZjnkVSP3vebO4Oaf/igYJ4SxTT0wMMZXFYZaSu
RqarcSBGXi2/zpxLp5q8+NL1g0KWW6Wgf5uBEDrBOEd9WGuwUTB5GXmnokc7Q3vf
ERPb47NrSRJZZjSvtP4/IzRrGz2DaaP2pMVwfuryzrr/FzTjG7G/C++TRcUYdcU6
ksib1Wm2omzPttSvT4k7ln7JhFwco1E/aS6z7GQoiPKbdDMljuHeIF5+4y7+OH9u
g97zAYcOcGG/rmgQnSxI+2d/NNkwe/oSaMiVpa8eExV7/3+uXifXot9BjGaPiyxu
6sbRNTUQxZvCv7QEfzR4
=Rwpi
-----END PGP SIGNATURE-----

--Apple-Mail=_C393CB53-614F-4929-9F35-8246084FC829--

.
