11/17/2012 5:22 PM, Michel Fortin пишет:
On 2012-11-16 18:56:28 +0000, Dmitry Olshansky <dmitry.o...@gmail.com>
Or wait a sec. Even simpler idiom and no extra features.
Drop the idea of 'access' taking a delegate. The other library idiom
is to return a RAII proxy that locks/unlocks an object on
construction/destroy.
with(lock(object_by_id))
{
... do what you like
}
Fine by me. And C++ can't do it ;)
Clever. But you forgot to access the variable somewhere. What's its name
within the with block?
Not having the name would imply you can't escape it :) But I agree it's
not always clear where the writes go to when doing things inside the
with block.
Your code would be clearer this way:
{
auto locked_object_by_id = lock(object_by_id);
// … do what you like
}
And yes you can definitely do that in C++.
Well, I actually did it in the past when C++0x was relatively new.
I just thought 'with' makes it more interesting. As to how access the
variable - it depends on what it is.
I maintain that the "synchronized (var)" syntax is still much clearer,
and greppable too. That could be achieved with an appropriate lowering.
Yes! If we could make synchronized to be user-hookable this all would be
more clear and generally useful. There was a discussion about providing
a user defined semantics for synchronized block. It was clear and useful
and a lot of folks were favorable of it. Yet it wasn't submitted as a
proposal.
All other things being equal I believe we should go in this direction -
amend a couple of things (say add a user-hookable synchronized) and
start laying bricks for std.sharing.
--
Dmitry Olshansky