Namespaces
Variants
Actions

Talk:cpp/thread/recursive mutex/try lock

From cppreference.com

Hi there,

I find the output of the provided example to be incorrect.

I executed the program using MSVC 2019 [16.6.30128.74] and found the output to be


lock acquired

lock acquired


This is rather what I expected, as the mutex is a recursive_mutex used in the same thread.

Please correct me if I'm wrong here.

Thanks,

Sbielkind (talk) 23:46, 13 July 2020 (PDT)

you're right, an earlier edit to the example didn't update output to match --Cubbi (talk) 06:49, 14 July 2020 (PDT)

Hi,

The current example does not highlight the recursive nature of try_lock(). Rather it locks and unlocks two times in sequence. Unlike what the comment on line 14 says, this behavior would be identical to std::mutex. You can try this yourself.

--ArthurM (talk) 20 Sep 2020