2

In an application i need to periodically read the monotonic and system clock of std::chrono right after each other with the smallest possible delay inbetween.

Like this:

#include <iostream>
#include <chrono>

int main() {

    bool interrupted = false;
    auto time_system    = std::chrono::system_clock::now();
    auto time_monoton   = std::chrono::steady_clock::now();

    if (interrupted) {
        std::cout << "Thread had been interrupted between clock reading!" << std::endl;
    }
    else {
        //do stuff
    }

    return 0;
}

Of course there is still a small chance that the program is interrupted by the operating system right inbetween capturing the clock values and then time_system and time_monoton will not be aquired directly after each other.

Is there e.g. a thread status variable available to reset before and check if a thread has been interrupted by the operating system inbetween a program section.

I found that boost::threads can throw a boost::thread_interrupted-Exception, but i don't fully understand if this Exception is only thrown when some other thread activly calls the interrupt()-method or also when the OS is interrupting the prgroams process and their threads.

Is there a method within the STL or Boost to detect if a program got interrupted? (Besides the trivial solution to check monotonic clock before and after and define a reasonable threshold on a practical bases)

  • Being interrupted isn't something that happens inside the scope of the thread, for most purposes - it's really something the CPU does and deliberately hides from you. Even if the thread _isn't_ de-scheduled, or migrated to another core, an interrupt could occur between two lines and directly return control – Useless Aug 24 '21 at 11:46
  • If you want to control this stuff, you have some platform-specific work to do (eg, cpu isolation, scheduler class & priority). If you just want to detect it ... you could just take several consecutive snapshots of each timestamp and see if they're consistent – Useless Aug 24 '21 at 11:48
  • So what do you actually want - to detect whether some type of interruption occurred? What type, just descheduling or any interrupt? Or do you want to prevent the problem arising? – Useless Aug 24 '21 at 11:49

1 Answers1

3

No modern, general purpose operating systems provide this kind of a mechanism, to notify their execution threads every time they get pre-empted; and there's certainly nothing in the C++ core library that does this. This is outside the scope of the C++ library. In fact, operating systems go out of their way to give each execution thread an illusion that it's running continuously.

There are some vanity, special-purpose operating systems that provide real-time guarantees to running processes with dedicated, uninterrupted CPU time to their execution threads.

This kind of a capability must be designed into the operating system itself. There is no standardized C++ interface for it. If this is for the real-time operating system you're using (RTOS), you will need to consult its documentation for more information. If you need this kind of a capability for your program your best choice is to use an operating system that's designed to support it.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148