T
Tony Johansson
Hi!
At the bottom I have a class named Counter and in a method called
UpdateCount I use this statement
lock(this) to synchronize so that only one thread can access this section of
code. In class Test is main located also listed at the end.
I made a test to access method Foo in this class Counter using the same
object as I do when calling method UpdateCount
and it worked. First I was a litle uncertain if this statement lock(this)
will lock the object this in any way but it doesn't.
So accoding to my test this argument that is passed to lock here this which
is the currect Counter object is not locking
this Counter object in any way.
So what purpose has this argument that is passed to lock actually if it
doesn't lock the current object which I first thought..?
using System;
using System.Threading;
class Counter
{
private int _count;
private int _evenCount;
public int Count
{ get { return _count; } }
public int EvenCount
{ get { return _evenCount; } }
public void Foo()
{
System.Diagnostics.Trace.WriteLine("Before In Foo");
_count = 0;
System.Diagnostics.Trace.WriteLine("After In Foo");
}
public void UpdateCount()
{
lock(this)
{
_count = _count +1;
if (Count % 2 == 0)
_evenCount = _evenCount;
}
}
}
class Test
{
public static void Main()
{
Counter count = new Counter();
ParameterizedThreadStart starter = new
ParameterizedThreadStart(UpdateCount);
Thread[] threads = new Thread[10];
for (int x = 0; x < 10; x++)
{
threads[x] = new Thread(starter);
threads[x].Start(count);
}
for (int x = 0; x < 10; x++)
{
if (x == 0)
{
ParameterizedThreadStart starter2 = new
ParameterizedThreadStart(Foo);
Thread th = new Thread(starter2);
th.Start(count);
}
threads[x].Join();
}
Console.WriteLine("Total: {0} - EvenCount: {1}", count.Count,
count.EvenCount);
}
static void UpdateCount(object param)
{
Counter count = (Counter)param;
for (int x= 1; x <= 10000; x++)
count.UpdateCount();
}
static void Foo(object param)
{
Counter count = (Counter)param;
count.Foo();
}
}
At the bottom I have a class named Counter and in a method called
UpdateCount I use this statement
lock(this) to synchronize so that only one thread can access this section of
code. In class Test is main located also listed at the end.
I made a test to access method Foo in this class Counter using the same
object as I do when calling method UpdateCount
and it worked. First I was a litle uncertain if this statement lock(this)
will lock the object this in any way but it doesn't.
So accoding to my test this argument that is passed to lock here this which
is the currect Counter object is not locking
this Counter object in any way.
So what purpose has this argument that is passed to lock actually if it
doesn't lock the current object which I first thought..?
using System;
using System.Threading;
class Counter
{
private int _count;
private int _evenCount;
public int Count
{ get { return _count; } }
public int EvenCount
{ get { return _evenCount; } }
public void Foo()
{
System.Diagnostics.Trace.WriteLine("Before In Foo");
_count = 0;
System.Diagnostics.Trace.WriteLine("After In Foo");
}
public void UpdateCount()
{
lock(this)
{
_count = _count +1;
if (Count % 2 == 0)
_evenCount = _evenCount;
}
}
}
class Test
{
public static void Main()
{
Counter count = new Counter();
ParameterizedThreadStart starter = new
ParameterizedThreadStart(UpdateCount);
Thread[] threads = new Thread[10];
for (int x = 0; x < 10; x++)
{
threads[x] = new Thread(starter);
threads[x].Start(count);
}
for (int x = 0; x < 10; x++)
{
if (x == 0)
{
ParameterizedThreadStart starter2 = new
ParameterizedThreadStart(Foo);
Thread th = new Thread(starter2);
th.Start(count);
}
threads[x].Join();
}
Console.WriteLine("Total: {0} - EvenCount: {1}", count.Count,
count.EvenCount);
}
static void UpdateCount(object param)
{
Counter count = (Counter)param;
for (int x= 1; x <= 10000; x++)
count.UpdateCount();
}
static void Foo(object param)
{
Counter count = (Counter)param;
count.Foo();
}
}