469,628 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

if threads don't Sleep, synchronization will fail to occur ?!

OK, if you run this code below (Console app) you will find that threads will
not always behave in a synchronized fashion, even with a lock
statement, --unless you put a Sleep somewhere in the method passed to
ThreadStart.

Try it, and please prove me wrong.

********
using System;
using System.Threading;
namespace TestLockPrivate
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
innerClass2 a = new innerClass2();
a.startme();
innerClass2 b = new innerClass2();
b.startme();
innerClass2 c = new innerClass2();
c.startme();
}

public class innerClass1 {
private object alock = new object();
private static string str = "";
public void foo() {
lock(alock) {
str = Thread.CurrentThread.GetHashCode().ToString();
if (str !=
Thread.CurrentThread.GetHashCode().ToString()) {
Console.WriteLine("str = " + str + "
Thread.CurrentThread.GetHashCode().ToString() = " +
Thread.CurrentThread.GetHashCode().ToString());
}
}
}
}

public class innerClass2 {
public Thread t = null;
public void startme() {
t = new Thread(new ThreadStart(threadMain));
t.Start();
}
public void threadMain() {
while (true) {
innerClass1 l = new innerClass1();
l.foo();
}
}
}
}
}
Nov 15 '05 #1
2 1585

You are right! I added the static modifier to alock and now it is
synchronized.

But, can you please explain 3) below. I don't understand.
"Willy Denoyette [MVP]" <wi*************@skynet.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
pokémon wrote:
|| OK, if you run this code below (Console app) you will find that
|| threads will not always behave in a synchronized fashion, even with
|| a lock
|| statement, --unless you put a Sleep somewhere in the method passed to
|| ThreadStart.
||
|| Try it, and please prove me wrong.
||
|| ********
|| using System;
|| using System.Threading;
|| namespace TestLockPrivate
|| {
|| class Class1
|| {
|| [STAThread]
|| static void Main(string[] args)
|| {
|| innerClass2 a = new innerClass2();
|| a.startme();
|| innerClass2 b = new innerClass2();
|| b.startme();
|| innerClass2 c = new innerClass2();
|| c.startme();
|| }
||
|| public class innerClass1 {
|| private object alock = new object();
|| private static string str = "";
|| public void foo() {
|| lock(alock) {
|| str =
|| Thread.CurrentThread.GetHashCode().ToString();
|| if (str !=
|| Thread.CurrentThread.GetHashCode().ToString()) {
|| Console.WriteLine("str = " + str + "
|| Thread.CurrentThread.GetHashCode().ToString() = " +
|| Thread.CurrentThread.GetHashCode().ToString());
|| }
|| }
|| }
|| }
||
|| public class innerClass2 {
|| public Thread t = null;
|| public void startme() {
|| t = new Thread(new ThreadStart(threadMain));
|| t.Start();
|| }
|| public void threadMain() {
|| while (true) {
|| innerClass1 l = new innerClass1();
|| l.foo();
|| }
|| }
|| }
|| }
|| }

Well, you are wrong :-).
1. Each thread creates an instance of innerClass1 and foo() puts a lock on the object refered to by alock, but as this object is not shared every thread is free to enter foo().
2. Otherwise, str is a shared object reference (static) and not protected by a lock so each thread running foo() is free to toy with str.....
3. Now if you would synchronize access to str, running foo() in such a closed loop allmost prevents other threads to run at the end of the running threads quota.

Willy.

Nov 15 '05 #2
Thanks for the explanation. It is very interesting to get such a low-level
understanding of what's going on in the processor. BTW, is it a bad thing
for the threads to run much longer then their quantum?

"Willy Denoyette [MVP]" <wi*************@skynet.be> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
pokémon wrote:
|| You are right! I added the static modifier to alock and now it is
|| synchronized.
||
|| But, can you please explain 3) below. I don't understand.
||
||
|| "Willy Denoyette [MVP]" <wi*************@skynet.be> wrote in message
|| news:%2****************@tk2msftngp13.phx.gbl...
||| 3. Now if you would synchronize access to str, running foo() in
||| such a
|| closed loop allmost prevents other threads to run at the end
||| of the running threads quota.
|||
||| Willy.

Each thread in the Windows system is allowed to run for an amount of time, called a quantum (quantum's can vary per thread and OS versions), before Windows interrupts the thread to see whether another thread (with higher or equal priority) is ready to run. Now if you take a look at your code, you have 3 runable threads (I don't consider the other threads in the process). While one thread is running the others are ready to run, but the running thread will run a large amount of it's quantum while holding a shared lock (running foo()) as such preventing the other two threads to run when scheduled (at the end of the running threads quantum) when the lock is still held (during foo()). The net result is that each thread will run for a much longer time than it's quantum (seconds i.s.o milliseconds).

Willy.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by andrewpalumbo | last post: by
5 posts views Thread by Michele Simionato | last post: by
6 posts views Thread by sathyashrayan | last post: by
7 posts views Thread by Mr. Mountain | last post: by
10 posts views Thread by [Yosi] | last post: by
7 posts views Thread by David Rushby | last post: by
12 posts views Thread by Dave | last post: by
23 posts views Thread by =?GB2312?B?0rvK18qr?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.