How can I check if any thread is asleep? and how to wake them up?

Following is the question,

"Implement an "alarm clock" class. Threads call "Alarm:GoToSleepFor(int howLong)" to go to sleep for a period of time. The alarm clock can be implemented using the hardware Timer device (i.e. timer.h). When the timer interrupt goes off, the Timer interrupt handler checks to see if any thread that had been asleep needs to wake up now. There is no requirement that threads start running immediately after waking up; just put them on the ready queue after they have waited for the approximately the right amount of time. The person will set an alarm and it will display a message after the set amount of time. Provide the options to snooz alarm for 10 sec or stop the alarm as well."

Can anyone help me solve this?
What I don't get is how can I use the Timer Interrupt Handler to check if any thread that is already asleep needs to be waken up?
and then how do I wake that up?
I am totally new to Linux and can't understand most of the things.... So, if anyone can provide the possible commands, libraries etc I can use to solve this then I would be really thankful
Last edited on
How much of what you're wanting to do is already covered by sleep?
http://man7.org/linux/man-pages/man3/sleep.3.html
If you want to wake them up yourself, you can take manual control with condition variables.
https://en.cppreference.com/w/cpp/thread/condition_variable
Just use sleep. The only tricky part is that sleep can return early, so you need to do something like:
1
2
3
4
5
6
7
void waitUntil (time_t when)
{
    time_t now;
    while (now= time(0), now < when) {
         sleep(when-now);
    }
}


For finer control you might use nanosleep() if available.
Topic archived. No new replies allowed.