
Eric Niebler wrote:
alnsn@yandex.ru wrote:
This is a degenerate case of more general idea of passing lock reference to functions inside condition:
if(lock_ref l = scoped_lock(m)) { // do something with l: action1(l); action2(l); }
Apparently, action1 and action2 can't be called without prior obtaining of a lock. Plus, you get some sort of polymorphism because the functions can accept other locks as well:
action1(scoped_try_lock(m1)); // lock_ref is const reference action2(scoped_timed_lock(m2));
Oh yeah, I heard a talk by Andrei Alexandrescu in which he endorsed that idiom. (Google for "Honey I shrunk the threads".) I like it, but I've never used it. :-P
I know he gave a talk at ACCU this year but I missed the event. And there is nothing on his site yet. Not a problem anymore ;-) Though, I was able to download Kevlin's presentation. -- Alexander Nasonov