class DoWork: public Thread, public Timer
{
run()
{
wait(1000);
// This is thread's run part
//get lock here.
// get some work done
// notify that thread can exit now
}
timer()
{
// this is automatic call back timer
(1)stopThread(0); // thread will wait for no milisec
(2)stopThread(1000); // that will wait for 1 sec (1000 milisec )
(3)stopThread(-1); // thread will wait forever
stopTimer(); // that stops timer call back
}
DoWork()
{
startThread(); // this one start the thread immidiately
}
~DoWork()
{
if(isRunning())
(1)stopThread(0); // thread will wait for no milisec
(2)stopThread(1000); // that will wait for 1 sec (1000 milisec )
(3)stopThread(-1); // thread will wait forever
}
};
So My question is, what are differences betweeen all three calls for stopThread(int millisec) ?
Giving a 0 ms, crashes the application.
giving negative ms wait for forever, but it work also (when it should be used ?)
giving regular ms like 1000/2000 work well.
void stopThread (constint timeOutms)
{
// acquire lock
if (isRunning())
{
threadShouldExit(); // notify thread is going to exit
if (timeOutms!= 0)
threadToExit (timeOutms);
if (isRunning())
{
killThread(); // auther says it is very bad to kill thread like this
//because thread is still running
//remove thread entry
}
}
}
bool threadToExit (constint timeOutms )
{
constint itr = 5;
int count = timeOutms / itr ;
while (isunning())
{
if (timeOutms > 0 && --count < 0)
returnfalse;
sleep (itr);
}
returntrue;
}
inside Thread Run method, the work done is :
request some data to network server
connect, get data, show the data
I don't this what work is done here matters a lot, yeah it matter but still like we are doing some work to read file, or showing data and like that only....
Now can any one please explain me what should be right int ms to pass in stopThread() ?
That must have been done because when a 'master' wants to stop a thread, there a 3 possible scenarios :
The master tells the thread to stop when he is finished, so it gives it an infinite delay
The master gives it some time to finish properly, but not more than a certain amount
The master wants it to stop immediately, regardless of if it still has work to do.
(1)stopThread(0); // thread will wait for no milisec
The master wants it to stop immediately, regardless of if it still has work to do.
(2)stopThread(1000); // that will wait for 1 sec (1000 milisec )
The master gives it some time to finish properly, but not more than a certain amount
(3)stopThread(-1); // thread will wait forever
The master tells the thread to stop when he is finished, so it gives it an infinite delay
that is what you want to say ? right.
Now i want to know that exactly in which scenarios i can use each stopThread(0)/(1000)/(-1) ?
I can't be more specific thant that sorry, it all depends on what you want to do with your threads. Ask yourself the questions :
Do i need my thread to close immediatly when i ask it to?
Can it stop immediately or is it dangerous?
Can i give it a little time to try to finish properly if what it is doing is keeping it busy?
run()
{
wait(1000);
lockThread();
// Do request to network client for some data
// this request function is boolean one so
// When it returns true I consider work done
- - elseiffalse tryAgain for one more time and still false
threadShouldExit(); // when work done or false come two time
}
timer()
{
// it continues check whether thread should stop signal is true or not ?
// When threadShouldExit() is called in run and then come here
// stopThread is being called here.
// here i have already signaled that thread work is almost done.
? ? ? stopThread(1000); / ? ? ? stopThread(-1);
}
Now can you guide me what exactly shall we put there to be more effective ?