How do I stop a thread?

Basically I want to stop a thread so I can recreate with new information in mind.

The purpose of the code is to allow the user to input time without pause and for the program to alert the user once the earliest time has passed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void ThreadClass::printTime(tm time) {
    sleep_until(system_clock::from_time_t (timelocal(&time)));
    cout << "it is " << time << endl;
    lookForNextTime();
}

void ThreadClass::lookForNextTime() {
    /*find earliest time*/
    printTime(time);
}

int main() {
    ThreadClass myThreadClass;
    thread t(&ThreadClass::lookForNextTime, myThreadClass);
    /*Ask for user to supply more times.*/
    /*Add more times*/
}


Here's an example of an ideal situation: The program is set to alert the user in one hour. The user inputs two more times, one is two hours from now, and the other is tomorrow. The program alerts the user, then it finds the second shortest time (two hours from now) and alerts them in another hour. The program then finds the last time (tomorrow) and alerts them again.

Now here's the problem: What if the program is waiting to alert the user for a time next week? The user inputs a new time set for an hour later. Instead of alerting the user an hour later, the program is still waiting for next week to come. What I want the program to do is stop sleeping and restart its search to consider the new time(s) added.

1
2
3
4
5
6
7
8
int main() {
    ThreadClass myThreadClass;
    thread t(&ThreadClass::lookForNextTime, myThreadClass);
    /*Ask for user to supply more times.*/
    /*Add more times*/
    /*Stop thread*/
    /*Recreate thread*/
}


Stopping and restarting the thread is the first thing that comes to mind, but I'm open to other methods too.
So let me get this right, you have one sleeping thread for every alarm time you have set in the future?
No--at least I hope I don't have to resort to that, because that sounds like it'd cause issues. I only need to worry about the next alarm. Once the alarm goes off, it automatically looks for the next time to set as the alarm.

Hopefully I can use one thread to do this. I'm hoping that I can

1) Stop the current thread.

and

2) Start a new thread in its place, this way it will look through the times again to make sure it still has the appropriate time to set as an alarm.

Ideally, I'd like two threads to have their own loops.

"Main Thread": Ask user for time->Accept time->Place time in queue->repeat.

"Class Thread": Find shortest time in queue->set alarm->alert user->repeat.
Last edited on
How about making "Class Thread" wake up once per second/minute to examine the head of your alarm queue, decide whether the alarm needs to go off, and then go back to sleep.

If "Main Thread" keeps the alarm queue ordered by time, then "Class Thread" only has to examine the head of the queue.

Sure you can do "Stop and Start".
But you only need to stop the alarm thread if your main thread modifies the head of the queue.
So basically this?

1
2
3
4
5
6
std::this_thread::sleep_for (std::chrono::seconds(60))
//find shortest time.
if (currenttime >= shortestime) {
    //alert
}
//loop and sleep again 

Last edited on
Yes, something like that.
One last question...how do I properly deal with the thread when the program ends. I'm not familiar with join(), but you need the other thread to return in order for this to work, right? This can't happen in a loop so unless I'm misunderstanding how join() works, how would I properly exit?
Yes, you need the other thread to exit for join() to happen.

For me
- reduce the poll time to once per second
- when you decide to exit, purge the wait queue
- arrange the wait thread to automatically exit if the queue is empty
But how would I know when to start it again when it exits?
Topic archived. No new replies allowed.