Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is there lock_guard ?

158 views
Skip to first unread message

Bonita Montero

unread,
Feb 17, 2024, 2:15:52 PMFeb 17
to
Why is there lock_guard when unique_lock can anything lock_guard
can do but far more ?

Chris M. Thomasson

unread,
Feb 17, 2024, 6:40:27 PMFeb 17
to
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...




Bonita Montero

unread,
Feb 18, 2024, 1:59:21 AMFeb 18
to
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.

Chris M. Thomasson

unread,
Feb 18, 2024, 3:13:41 PMFeb 18
to
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...

Bonita Montero

unread,
Feb 19, 2024, 12:50:56 AMFeb 19
to
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.


Chris M. Thomasson

unread,
Feb 19, 2024, 2:56:47 PMFeb 19
to
lock_guard is highly restricted when compared to unique_lock... ;^)
0 new messages