Namespaces
Variants
Views
Actions

Talk:cpp/thread/mutex

From cppreference.com

Why is there a lock around the for loop? The threads are joined and not running anymore, no other thread could possibly interfere. Suggest removing them altogether.

You're correct that the loop lock isn't necessary. One argument for leaving it there is that the code maintains the invariant that all accesses of the (potentially) shared data happen behind a lock, which might be considered good style. If the extra lock makes the example confusing, however, it might be worth discarding it. Nate 17:29, 25 September 2012 (PDT)
I fixed that. --87.148.222.140 16:08, 12 January 2013 (PST)



The example for mutex should show mutex functionality. Even if mutex is normally used with lock_guard, etc. examples for doing so can be shown on the lock_guard page. If there are no objections within a few weeks, I'll make a change to that effect Swiss Frank (talk) 01:11, 18 May 2020 (PDT)

it does show mutex functionality. It's passed as the argument to a lock object, that's literally the first thing you should see about using std::mutex. Member function pages can show calls to those member functions. --Cubbi (talk) 06:47, 18 May 2020 (PDT)
 : Why not put in the textual description, and/or even in a comment in the example program, that this is the usual usage, then use this page about the mutex to show how the normally-avoided mutex functionality actually works? By all means direct people to lock_guard to see the typical usage, but let them see the untypical functionality actually offered by the mutex here. Either that, or have TWO example programs that both show the SAME thing, and don't illustrate the mutex at all. Which is more informative, to have duplicated copies of the same example, or to actually show functionality, no matter how rarely used, on the page of the object that's being described? Swiss Frank (talk) 00:22, 19 May 2020 (PDT)