[ROOT] deleting threads

From: sascha.zelzer@gmx.net
Date: Thu May 16 2002 - 17:10:04 MEST


Hi!

I am writing a GUI program which uses an extra thread to do a Monte-Carlo
algorithm ( the function "void Layer::MonteCarlo(void*)" ). For this I've got
two buttons to start and stop the thread, each calling a specific function:

Int_t Layer::MonteCarloStart()
{
	runSimulation = kTRUE;
	cout << "MonteCarloStart()" << endl;
	if (!mcThread) {
		mcThread = new TThread("MonteCarlo", (void(*) (void*))&MonteCarlo,
(void*)this);
		cout << "[MonteCarloStart()] ThreadState: " << mcThread->GetState() <<
endl;
		mcThread->Run();
		cout << "[MonteCarloStart()] ThreadState: " << mcThread->GetState() <<
endl;
		return 0;
	}
	
	return 1;
}

Int_t Layer::MonteCarloStop()
{
	runSimulation = kFALSE;
	TThread::Sleep(1);
	cout << "MonteCarloStop()" << endl;
	
	if (mcThread) {
		TThread::Delete(mcThread);
		if (mcThread) delete mcThread;
		cout << "[MonteCarloStop] thread deleted" << endl;
		mcThread = 0;
		return 0;
	}
	
	return 1;
}

If I do repeated Start/Stop sequences, it happens that the thread gets
created and the Run() function is executed, but somehow the MonteCarlo() function
isn't. Then MonteCarloStop() won't delete the thread and it rests in memory.
Sometimes this happens at the second Start/Stop, sometimes later. If anyone
got experiences with TThread and could point me in the right direction, I
would be really happy.

I am using ROOT v3.02.07 and POSIX threads.

Thanks,
Sascha

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:53 MET