stopThread(ms) after some work in thread

Hello All...

I want to know some information about thread , and how does it work if i use it like following...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.

Please explain. Thanks you friends...
Last edited on
That's impossible to answer without seeing:
1. what the thread is doing
2. the implementation of stopThread()
Ok so here goes my stopThread definition - from the original thread library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void stopThread (const int 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 (const int timeOutms )
{
    const int itr = 5;
    int count = timeOutms / itr ;

    while (isunning())
    {
        if (timeOutms > 0 && --count < 0)
            return false;
        sleep (itr);
    }
    return true;
}

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() ?
Last edited on
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.

Which one to use wil depend on your program
Hey bartoli.

So I think,

(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?

Yes i understand what you are trying to say.

In my scenario Inside thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 
    - - else if false 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 ?

Thanks for your explanations.
Topic archived. No new replies allowed.