473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

threads, static variables

Hi,

the following .NET 1.1 C#-program does not terminate (opposed to what I
would expect):

--------
using System;
using System.Threading;

public class Test {

static int i = 0;

public static void Main(string [] args) {
new Thread(new ThreadStart(t1)).Start();
new Thread(new ThreadStart(t2)).Start();
}

static void t1() {
int j = 0;
while(i == 0) {
j++;
}
}

static void t2() {
i = 1;
}

}
--------

A slight variation of the above program makes the strange behavior more
explicit:

--------
using System;
using System.Threading;

public class Test {

static int i1 = 0;
static int i2 = 0;

public static void Main(string [] args) {
new Thread(new ThreadStart(t1)).Start();
new Thread(new ThreadStart(t2)).Start();
}

static void t1() {
int j = 0;
i1 = 1;
while(i2 == 0) {
j++;
if(j + 1 < j) break;
}
Console.WriteLine(j);
}

static void t2() {
i2 = 1;
Console.WriteLine("t2: i1={0}, i2={0}", i1, i2);
}

}
--------
Output:
--------
t2: i1=1, i2=1
2147483647
--------

That means thread t2 sets the static variable i2=1, but thread t1 seems
to be unaware of this change of i2.

Can anyone reproduce this behavior in his environment with the above
programs (don't change a single line, because even slight variations
result in a different behavior)?

And what I'm interested more: Can anyone explain why these programs
behave in this way? Is this a bug in .NET/C#?

Nov 17 '05 #1
6 2496
<kr*****@gmx.net> wrote:
the following .NET 1.1 C#-program does not terminate (opposed to what I
would expect):
<snip>
That means thread t2 sets the static variable i2=1, but thread t1 seems
to be unaware of this change of i2.
Yup.
Can anyone reproduce this behavior in his environment with the above
programs (don't change a single line, because even slight variations
result in a different behavior)?
It doesn't happen on my box, but that's a single processor box - are
you by any chance running it on a multi-processor box?
And what I'm interested more: Can anyone explain why these programs
behave in this way? Is this a bug in .NET/C#?


No, it's not a bug in .NET. It's the way the .NET memory model works.
See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Inline

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<kr*****@gmx.net> wrote:
the following .NET 1.1 C#-program does not terminate (opposed to what I
would expect):


<snip>
That means thread t2 sets the static variable i2=1, but thread t1 seems
to be unaware of this change of i2.


Yup.
Can anyone reproduce this behavior in his environment with the above
programs (don't change a single line, because even slight variations
result in a different behavior)?


It doesn't happen on my box, but that's a single processor box - are
you by any chance running it on a multi-processor box?

The first sample won't terminate even on a single CPU when compiled with
full JIT optimization turned on (/o+ ), the static variable is register
allocated as opposed to heap allocated when not optimized (the default).
And what I'm interested more: Can anyone explain why these programs
behave in this way? Is this a bug in .NET/C#?


No, it's not a bug in .NET. It's the way the .NET memory model works.
See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Hi,

Jon wrote:
It doesn't happen on my box, but that's a single processor box - are
you by any chance running it on a multi-processor box?
One Pentium-4 with hyperthreading turned off.
No, it's not a bug in .NET. It's the way the .NET memory model works.
See http://www.pobox.com/~skeet/csharp/t...latility.shtml
Thanks a lot for that URL. Declaring i and i2 as "volatile" helped!
Willy Denoyette [MVP] wrote: The first sample won't terminate even on a single CPU when compiled with
full JIT optimization turned on (/o+ ), the static variable is register
allocated as opposed to heap allocated when not optimized (the default).


"/o+" or "/o-" made no difference on my computer (first sample didn't
terminate in both cases).

Nov 17 '05 #4

<kr*****@gmx.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,

Jon wrote:
It doesn't happen on my box, but that's a single processor box - are
you by any chance running it on a multi-processor box?


One Pentium-4 with hyperthreading turned off.
No, it's not a bug in .NET. It's the way the .NET memory model works.
See http://www.pobox.com/~skeet/csharp/t...latility.shtml


Thanks a lot for that URL. Declaring i and i2 as "volatile" helped!
Willy Denoyette [MVP] wrote:
The first sample won't terminate even on a single CPU when compiled with
full JIT optimization turned on (/o+ ), the static variable is register
allocated as opposed to heap allocated when not optimized (the default).


"/o+" or "/o-" made no difference on my computer (first sample didn't
terminate in both cases).


Try with /debug option on v1.1.4322, it should terminate. In this release
/o- is the default, and I guess it does register allocation per default
unless you have /debug specified.
Willy.
Nov 17 '05 #5
Willy Denoyette [MVP] <wi*************@telenet.be> wrote:
It doesn't happen on my box, but that's a single processor box - are
you by any chance running it on a multi-processor box?


The first sample won't terminate even on a single CPU when compiled with
full JIT optimization turned on (/o+ ), the static variable is register
allocated as opposed to heap allocated when not optimized (the default).


Ooh, indeed. That's great - a real sample I could include in my page,
which positively shows the dangers!

Cheers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@telenet.be> wrote:
> It doesn't happen on my box, but that's a single processor box - are
> you by any chance running it on a multi-processor box?
The first sample won't terminate even on a single CPU when compiled with
full JIT optimization turned on (/o+ ), the static variable is register
allocated as opposed to heap allocated when not optimized (the default).


Ooh, indeed. That's great - a real sample I could include in my page,
which positively shows the dangers!


Sure it is, note that I'm experimenting a bit with these settings, and there
seem to be a minor difference between v1.1 and v2 (the beta). In v2.0 /o- ,
keeps the static heap allocated, while /o+ does register allocation (in this
particular sample!). In V1.1 /o+ or /o- both use a register for the
variable. Moreover, that I notice this behavior is processor type dependent,
that is AMD Athlon64 and Intel P4 behave differently.
You see reasons enough to lock your shared state.
Cheers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeremie | last post by:
Hi, class A { <..> }; const A& GetA() { static A a;
6
by: ouech | last post by:
hi, I'd like to know if i need mutexs to lock the use of member methods of a class instance shared between several threads via a pointer? if the method modify member variables, i know that...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
5
by: Steve - DND | last post by:
I've seen many threads on Google about questions regarding stateless classes, and thread synchronization. However, either I am dense, or I have just not found the right thread, but I'm still not...
7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
1
by: chunghorng_lung | last post by:
Hi All, I have a question on the scope of variables for threads. The program has a main thread which creates a few worker threads. The main thread can access another class stored in another...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.