K
kracchh
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#?
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#?