xtd 0.2.0
Loading...
Searching...
No Matches
timeout.cpp

Shows how to use xtd::threading::timeout class.

#include <xtd/threading/interlocked>
#include <xtd/threading/thread>
#include <xtd/threading/thread_interrupted_exception>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace timeout_example {
class stay_awake {
public:
stay_awake() = default;
void sleep_switch(bool value) {
sleep_switch_ = value;
}
void thread_method() {
console::write_line("new_thread is executing thread_method.");
while(!sleep_switch_) {
// Use SpinWait instead of Sleep to demonstrate the
// effect of calling Interrupt on a running thread.
thread::spin_wait(10000000);
}
try {
console::write_line("new_thread going to sleep.");
// When new_thread goes to sleep, it is immediately
// woken up by a ThreadInterruptedException.
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);
// The following line causes an exception to be thrown
// in thread_method if new_thread is currently blocked
// or becomes blocked in the future.
// This method only work on Windows Operating System.
new_thread.interrupt();
console::write_line("main thread calls interrupt on new_thread.");
// Tell new_thread to go to sleep.
stay_awake.sleep_switch(true);
// Wait for new_thread to end.
new_thread.join();
}
};
}
startup_(timeout_example::thread_interrupt::main);
// This example produces output similar to the following on Windows Operating System only:
//
// new_thread is executing thread_method.
// main thread calls interrupt on new_thread.
// new_thread going to sleep.
// new_thread cannot go to sleep - interrupted by main thread.
The exception that is thrown when a Thread is interrupted while it is in a waiting state.
Definition thread_interrupted_exception.h:22
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:41
void start()
Causes the operating system to change the state of the current instance to xtd::threading::thread_sta...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:166
delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition thread_start.h:22
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10