Hello.
I did an example of my threading. The problem is, that the while loop is using 100% of cpu. Is there any another way to do that?
The notify has to be statical.
My code:
using System;
using System.Threading;
using System.Diagnostics;
namespace ConsoleApplication2
{
class ThreadingExample
{
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
test a = new test(1);
test b = new test(2);
test c = new test(3);
Thread t = new Thread(a.work);
Thread tb = new Thread(b.work);
Thread tc = new Thread(c.work);
t.Start();
tb.Start();
tc.Start();
Console.WriteLine("main thrad sleeping for 1 second...");
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...2");
test.notify(2);
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...1");
test.notify(1);
}
}
class test
{
private int i;
static int who;
static bool free;
private int pwho;
public test(int j)
{
i=j;
}
public void work()
{
Console.WriteLine(" waiting on event... {0}",i);
while(pwho != i)
{
//do nothing just waiting
}
pwho = who;
free = false;
Console.WriteLine(" worker thread reactivated, now exiting...{0}",i);
}
static public void notify(int w)
{
while(free){}
free = true;
who = w;
}
}
}