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#? 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
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
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).
<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.
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jeremie |
last post by:
Hi,
class A
{
<..>
};
const A& GetA()
{
static A a;
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
|
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...
| | |