#include <xtd/xtd>
namespace timeout_example {
class stay_awake {
public:
stay_awake() = default;
void sleep_switch(bool value) {
sleep_switch_ = value;
}
void thread_method() {
while(!sleep_switch_) {
}
try {
thread::sleep(timeout::infinite);
} catch(const thread_interrupted_exception& e) {
console::write_line("new_thread cannot go to sleep - "
"interrupted by main thread.");
}
}
private:
bool sleep_switch_ = false;
};
class thread_interrupt {
public:
static void main() {
auto stay_awake = timeout_example::stay_awake {};
auto new_thread = thread {
thread_start {stay_awake, &timeout_example::stay_awake::thread_method}};
new_thread.start();
thread::sleep(10);
new_thread.interrupt();
console::write_line("main thread calls interrupt on new_thread.");
stay_awake.sleep_switch(true);
new_thread.join();
}
};
}
startup_(timeout_example::thread_interrupt::main);
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static void spin_wait(int32 iterations)
Causes a thread to wait the number of times defined by the iterations parameter.
xtd::delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition thread_start.hpp:24
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168