• Why is there lock_guard ?

    From Bonita Montero@3:633/280.2 to All on Sun Feb 18 06:15:33 2024
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Chris M. Thomasson@3:633/280.2 to All on Sun Feb 18 10:40:15 2024
    On 2/17/2024 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    lock_guard is meant for its purpose. Only use unique_lock when you need
    to. And be careful, you can shoot yourself in the foot with it...





    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Bonita Montero@3:633/280.2 to All on Sun Feb 18 17:59:10 2024
    Am 18.02.2024 um 00:40 schrieb Chris M. Thomasson:
    On 2/17/2024 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    lock_guard is meant for its purpose. ...

    You easily could do ...

    template<typename Lock>
    using std::lock_guard = std::unique_lock<Lock>;

    .... without any problems.

    For me lock_guard doesn't make sense.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Chris M. Thomasson@3:633/280.2 to All on Mon Feb 19 07:13:26 2024
    On 2/17/2024 10:59 PM, Bonita Montero wrote:
    Am 18.02.2024 um 00:40 schrieb Chris M. Thomasson:
    On 2/17/2024 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    lock_guard is meant for its purpose. ...

    You easily could do ...

    ˙˙˙˙template<typename Lock>
    ˙˙˙˙using std::lock_guard = std::unique_lock<Lock>;

    ... without any problems.

    For me lock_guard doesn't make sense.

    It has its purpose. If you did this then std::lock_guard would be
    moveable even though its not meant to be. That's just one point...

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Bonita Montero@3:633/280.2 to All on Mon Feb 19 16:50:38 2024
    Am 18.02.2024 um 21:13 schrieb Chris M. Thomasson:
    On 2/17/2024 10:59 PM, Bonita Montero wrote:
    Am 18.02.2024 um 00:40 schrieb Chris M. Thomasson:
    On 2/17/2024 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    lock_guard is meant for its purpose. ...

    You easily could do ...

    ˙˙˙˙˙template<typename Lock>
    ˙˙˙˙˙using std::lock_guard = std::unique_lock<Lock>;

    ... without any problems.

    For me lock_guard doesn't make sense.

    It has its purpose. ...

    .... which unique_lock also fulfills.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Chris M. Thomasson@3:633/280.2 to All on Tue Feb 20 06:56:34 2024
    On 2/18/2024 9:50 PM, Bonita Montero wrote:
    Am 18.02.2024 um 21:13 schrieb Chris M. Thomasson:
    On 2/17/2024 10:59 PM, Bonita Montero wrote:
    Am 18.02.2024 um 00:40 schrieb Chris M. Thomasson:
    On 2/17/2024 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    lock_guard is meant for its purpose. ...

    You easily could do ...

    ˙˙˙˙˙template<typename Lock>
    ˙˙˙˙˙using std::lock_guard = std::unique_lock<Lock>;

    ... without any problems.

    For me lock_guard doesn't make sense.

    It has its purpose. ...

    ... which unique_lock also fulfills.



    lock_guard is highly restricted when compared to unique_lock... ;^)

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Andrey Tarasevich@3:633/280.2 to All on Fri Mar 1 15:50:47 2024
    On 02/17/24 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    If you really want to ask about it, you should probably consider
    `scoped_lock` instead of `lock_guard` anyway. The rest can be found here

    https://stackoverflow.com/a/60172828

    --
    Best regards,
    Andrey

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Bonita Montero@3:633/280.2 to All on Sat Mar 2 00:49:13 2024
    Am 01.03.2024 um 05:50 schrieb Andrey Tarasevich:
    On 02/17/24 11:15 AM, Bonita Montero wrote:
    Why is there lock_guard when unique_lock can anything lock_guard
    can do but far more ?

    If you really want to ask about it, you should probably consider `scoped_lock` instead of `lock_guard` anyway. The rest can be found here

    scoped_lock is only necessary if you need to lock multiple mutexes. The
    mutexes are internally sorted according to their addresses, resulting in
    no deadlocks as long as all competing threads do it the same way. You
    can not defer locking or adopt already locked mutexes with scoped_lock,
    so there's something essentially missing with scoped_lock.


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)