W
William Stacey [MVP]
What if we had some kind on declarative synchronization where you could
declare, in declaration section, different lock sets for your invariant?
Something like:
public class MyClass
{
lock(sync1) // All three vars protected by same lock declaratively
(via internal object named sync1)..
{
private int counter1;
private int counter2;
private object obj;
}
lock(sync2) // These vars protected by a different lock and atomic
together.
{
private int someother;
private string desc;
}
private readonly string id = "123"; // no lock required.
public MyClass() {}
public string Name
{
get{ return name;}
set{ name = value;}
}
public int Counter1
{
get { return counter1; }
set { counter1 = value; }
}
}
Now you don't need lock blocks all over the place, but still get same
locking semantics when compiled. Also, I think this would help write better
code as you define your locking first based on your data (i.e. invariant
contract) and you can't forget to lock something and it reduces clutter and
number of lines you need to write. Maybe they could handle rw locks same
kind of way. Other ideas?
declare, in declaration section, different lock sets for your invariant?
Something like:
public class MyClass
{
lock(sync1) // All three vars protected by same lock declaratively
(via internal object named sync1)..
{
private int counter1;
private int counter2;
private object obj;
}
lock(sync2) // These vars protected by a different lock and atomic
together.
{
private int someother;
private string desc;
}
private readonly string id = "123"; // no lock required.
public MyClass() {}
public string Name
{
get{ return name;}
set{ name = value;}
}
public int Counter1
{
get { return counter1; }
set { counter1 = value; }
}
}
Now you don't need lock blocks all over the place, but still get same
locking semantics when compiled. Also, I think this would help write better
code as you define your locking first based on your data (i.e. invariant
contract) and you can't forget to lock something and it reduces clutter and
number of lines you need to write. Maybe they could handle rw locks same
kind of way. Other ideas?