I was trying to add threading to a static class that I have and came across a bunch of problems. I read this thread and the blog post that it links to and I think I understand what's going on. But I can't figure out why the Parallel For loop still works as in this example:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadingTest
{
public static class TestClass
{
public static int AwesomeNum = 43;
static TestClass()
{
string[] x = { "deal", "witch", "panda"};
//does not cause a deadlock? huh?
Parallel.For(0, x.Length, i =>
{
Console.WriteLine(x[i]);
});
//results in a deadlock
//Parallel.Invoke(writesomething, writesomethingelse);
//results in deadlock
Thread thread = new Thread(new ThreadStart(() =>
{
Console.WriteLine("there is a bear in my soup");
}));
thread.Start();
thread.Join();
}
private static void writesomething()
{
Console.WriteLine("writing something");
}
private static void writesomethingelse()
{
Console.WriteLine("writing something else.");
}
}
}
using System;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TestClass.AwesomeNum.ToString());
}
}
}