I have found that threads created in static contexts, at least when called from the program entry point, are blocked by those created in a class when run in non-debug mode but seem to operate just fine when debugging. Here’s the code and output:
Program.cs:
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.IO;
namespace FEZ_Cobra_Console_Application1
{
public class Program
{
public static void Main()
{
new TestClass();
while (true)
{
for (int i = 0; i < 10000; i++)
;
Debug.Print("2");
}
}
}
}
TestClass.cs:
using System;
using Microsoft.SPOT;
using System.Threading;
namespace FEZ_Cobra_Console_Application1
{
class TestClass
{
public TestClass()
{
Thread Thread1;
Thread1 = new Thread(TestMethod1);
Thread1.Start();
}
private void TestMethod1()
{
while (true)
{
for (int i = 0; i < 100000; i++)
;
Debug.Print("1");
}
}
}
}
Output in Debug:
2
2
2
2
2
2
2
2
2
2
1
2
2
2
2
2
2
2
2
2
2
1
2
2
2
2
2
2
2
2
2
2
2
1
2
2
Output not in debug:
2
1
1
1
1
1
1
1
1
1
1
1
1
Then if I change Program.cs to this:
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.IO;
namespace FEZ_Cobra_Console_Application1
{
public class Program
{
public static void Main()
{
new Thread(() =>
{
while (true)
{
for (int i = 0; i < 10000; i++)
;
Debug.Print("3");
}
}).Start();
new TestClass();
while (true)
{
for (int i = 0; i < 10000; i++)
;
Debug.Print("2");
}
}
}
}
The outputs are,
Debug:
3
2
3
2
3
2
3
2
3
2
3
2
3
2
3
2
3
2
2
3
1
2
3
2
3
2
3
2
non-debug:
2
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
In my experimentation if the blocking thread returns the blocked thread starts executing. I think this is linked to static contexts as I ran across this behavior when creating singleton instances in the program entry point by calling static getters that instantiate non-static objects which create threads. I haven’t done a lot of experimentation to pin it on this for sure though.
Thanks