S
Sunny
Hi,
I can not understend completely the lock statement.
Actally what is locked:
1. the part of the code between {...}
or
2. the object in lock()
In the docs is written:
for 1: The lock keyword marks a statement block as a critical section by
obtaining the mutual-exclusion lock for a given object, executing a
statement, and then releasing the lock. lock ensures that one thread
does not enter a critical section while another thread is in the
critical section of code. If another thread attempts to enter a locked
code, it will wait (block) until the object is released.
for 2: Typically, expression will either be this, if you want to protect
an instance variable, or typeof(class), if you want to protect a static
variable (or if the critical section occurs in a static method in the
given class).
What is happening if:
public class MyClass
{
int x;
public void MyAMethod()
{
lock(this)
{
x++;
}
}
public void MyBMethod()
{
lock(this)
{
x--;
}
}
public void MyCMethod()
{
x = x*x;
}
}
Now, if I create
static MyClass myInstance = new MyClass();
and in different threads I invoke different methods of myInstance what
will happen? Will MyCMethod (without lock) change x while other thread
has a lock over the object, or will wait until lock is released?
And if I have:
public class MyClass
{
public static int[] arr;
public static int[] otherobject;
...some nonstatic methods and vars
}
if in different threads I have different instances of MyClass, and while
executing different methods, may I lock only arr?
I.e.:
Thread1:
class test1
{
MyClass inst1 = new MyClass;
public void Test1Method()
{
lock (MyClass.arr)
{ .code1. }
}
}
Thread2:
class test2
{
MyClass inst2 = new MyClass;
public void Test2Method()
{
lock (MyClass.arr)
{ .code2. }
}
}
Thread3:
class test3
{
MyClass inst3 = new MyClass;
public void Test3Method()
{
MyClass.otherobject[8] = 11;
MyClass.arr[3] = 5;
}
}
Will Test3Method wait for changing the value, while other thread release
MyClass.arr? And if so, if there are more static objects in MyClass, but
lock is only over one of them, where Test3Method will stop for waiting?
Thanks
Sunny
I can not understend completely the lock statement.
Actally what is locked:
1. the part of the code between {...}
or
2. the object in lock()
In the docs is written:
for 1: The lock keyword marks a statement block as a critical section by
obtaining the mutual-exclusion lock for a given object, executing a
statement, and then releasing the lock. lock ensures that one thread
does not enter a critical section while another thread is in the
critical section of code. If another thread attempts to enter a locked
code, it will wait (block) until the object is released.
for 2: Typically, expression will either be this, if you want to protect
an instance variable, or typeof(class), if you want to protect a static
variable (or if the critical section occurs in a static method in the
given class).
What is happening if:
public class MyClass
{
int x;
public void MyAMethod()
{
lock(this)
{
x++;
}
}
public void MyBMethod()
{
lock(this)
{
x--;
}
}
public void MyCMethod()
{
x = x*x;
}
}
Now, if I create
static MyClass myInstance = new MyClass();
and in different threads I invoke different methods of myInstance what
will happen? Will MyCMethod (without lock) change x while other thread
has a lock over the object, or will wait until lock is released?
And if I have:
public class MyClass
{
public static int[] arr;
public static int[] otherobject;
...some nonstatic methods and vars
}
if in different threads I have different instances of MyClass, and while
executing different methods, may I lock only arr?
I.e.:
Thread1:
class test1
{
MyClass inst1 = new MyClass;
public void Test1Method()
{
lock (MyClass.arr)
{ .code1. }
}
}
Thread2:
class test2
{
MyClass inst2 = new MyClass;
public void Test2Method()
{
lock (MyClass.arr)
{ .code2. }
}
}
Thread3:
class test3
{
MyClass inst3 = new MyClass;
public void Test3Method()
{
MyClass.otherobject[8] = 11;
MyClass.arr[3] = 5;
}
}
Will Test3Method wait for changing the value, while other thread release
MyClass.arr? And if so, if there are more static objects in MyClass, but
lock is only over one of them, where Test3Method will stop for waiting?
Thanks
Sunny