mercredi 17 septembre 2014

c# thread infinite loop


Vote count:

0




/* I am trying to run two set of threads using AutoResetEvent to coordinate with each other; After the first set (customer) is done, I used thread.join() to make sure all threads in the first set is done, the set the flag to stop the second thread. However, the thread.join() never complete and the debugger lost its track in between. The flag was never set so it keeps running. Can someone please see what goes wrong here? Thanks!


*/



private static AutoResetEvent tellerFree = new AutoResetEvent(true);
private volatile static bool doneflag = true;
public static void runMultTeller()
{

List<Thread> custThreads = new List<Thread>();
List<Thread> tellThreads = new List<Thread>();
for (int i = 1; i <= 50; i++)
{
Thread td = new Thread(getTeller);
td.Name = Convert.ToString(i);
custThreads.Add(td);
td.Start();
}

for (int j = 1; j <= 5; j++)
{
Thread tt = new Thread(doTelling);
tt.Name = Convert.ToString(j);
custThreads.Add(tt);
tt.Start();
}

foreach (Thread tc in custThreads)
{
if (tc.IsAlive)
{
tc.Join();
}
}
Console.WriteLine("Customer are done");

doneflag = false;

foreach (Thread t2 in tellThreads)
{
t2.Join();
}
Console.WriteLine("Teller are done");

Console.WriteLine("Done");
Thread.Sleep(5000);
}

static public void doTelling()
{
string name = Thread.CurrentThread.Name;
while (doneflag)
{
Console.WriteLine("teller#{0} serving", name);
Thread.Sleep(500);
Console.WriteLine("teller#{0} done", name);
tellerFree.Set();
}
}

static public void getTeller()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine("customer#{0} Enter", name);
tellerFree.WaitOne();
Console.WriteLine("customer#{0} Leave", name);

}


asked 15 secs ago







c# thread infinite loop

Aucun commentaire:

Enregistrer un commentaire