B
beginwithl
hi
1) Say we have an instance A1 of type A. Now if A1 has for example 10
methods, of which two (A_m1 and A_m2) can be locked ( in both methods
we pass ‘this’ as a lock token ), while the remaining methods don’t
use locks, then even if thread T0 calls A_m1 on A, other threads are
still able to call non-locked methods on A( though they get blocked on
calling A_m2()).
Similary, if both threads T0 and T1 call A1.A_m1() at the same time,
but use different object as a lock token, neither of the two threads
will be in blocking mode
a) why, when “this” is used as lock token, isn’t the whole object
locked, and not just methods with locks inside them?
Wouldn’t it be more safe, since some other thread may be using some
other means to access and operate on same members of A1 as A1.A_m1()
is, and thus data may become corrupted?
BTW - I know that one could argue that it is up to the programmer to
make sure that particular data only gets accessed through A1.A_m1(),
but then we could reason as to why use locks at all, since it should
be up to programmer to make sure data doesn’t get corrupted.
b) even if we pass to A1.A_m1() as a lock token some other instance,
and not “this”, wouldn’t it be best if A1 was locked also?
c) say object B is passed as a token to A1.A_m1(). Wouldn’t it make
sense that B was also locked? Afterall, some other thread may be
changing members of B ( not via A1.A_m1 ) at the same time as A1.A_m1
() is operating on those same B members?!
2) Book suggests that if you are locking down a region of code within
apublic member, it is safer to declare a private object member
variable to serve as a token:
class Printer
{
private object threadLock = new object();
public void PrintNumbers()
{
lock ( threadLock )
{ …}
}
}
Now why would having a public token in a public method represent a
security risk:
class Printer
{
public void PrintNumbers()
{
lock ( this )
{
}
}
}
thank you
1) Say we have an instance A1 of type A. Now if A1 has for example 10
methods, of which two (A_m1 and A_m2) can be locked ( in both methods
we pass ‘this’ as a lock token ), while the remaining methods don’t
use locks, then even if thread T0 calls A_m1 on A, other threads are
still able to call non-locked methods on A( though they get blocked on
calling A_m2()).
Similary, if both threads T0 and T1 call A1.A_m1() at the same time,
but use different object as a lock token, neither of the two threads
will be in blocking mode
a) why, when “this” is used as lock token, isn’t the whole object
locked, and not just methods with locks inside them?
Wouldn’t it be more safe, since some other thread may be using some
other means to access and operate on same members of A1 as A1.A_m1()
is, and thus data may become corrupted?
BTW - I know that one could argue that it is up to the programmer to
make sure that particular data only gets accessed through A1.A_m1(),
but then we could reason as to why use locks at all, since it should
be up to programmer to make sure data doesn’t get corrupted.
b) even if we pass to A1.A_m1() as a lock token some other instance,
and not “this”, wouldn’t it be best if A1 was locked also?
c) say object B is passed as a token to A1.A_m1(). Wouldn’t it make
sense that B was also locked? Afterall, some other thread may be
changing members of B ( not via A1.A_m1 ) at the same time as A1.A_m1
() is operating on those same B members?!
2) Book suggests that if you are locking down a region of code within
apublic member, it is safer to declare a private object member
variable to serve as a token:
class Printer
{
private object threadLock = new object();
public void PrintNumbers()
{
lock ( threadLock )
{ …}
}
}
Now why would having a public token in a public method represent a
security risk:
class Printer
{
public void PrintNumbers()
{
lock ( this )
{
}
}
}
thank you