xtd 0.2.0
xtd::threading::timeout Class Reference

Definition

Contains a constant used to specify an infinite amount of time. This class cannot be inherited.

class core_export_ timeout static_
Inheritance
xtd::static_objectxtd::threading::timeout
Header
#include <xtd/threading/timeout>
Namespace
xtd::threading
Library
xtd.core
Example
The following example shows a thread going to sleep for an infinite time and subsequently being woken up. As the xtd::threading::thread::interrupt method only works on the Windows operating system, on other platforms the result may be different.
#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.
} 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();
// 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.

Fields

static constexpr int32 infinite = -1
 A constant used to specify an infinite waiting period. This field is constant. More...
 
static const time_span infinite_time_span
 A constant used to specify an infinite waiting period, for methods that accept a TimeSpan parameter. More...
 

Member Data Documentation

◆ infinite

constexpr int32 xtd::threading::timeout::infinite = -1
static

A constant used to specify an infinite waiting period. This field is constant.

Remarks
The value of this constant is -1. For threading methods that accept a timeout parameter, such as Thread::Sleep(int32) and Thread::Join(int32), this value is used to suppress timeout behavior.
The value of this field is -1 (0xFFFFFFFF).
Examples:
timeout.cpp.

◆ infinite_time_span

const time_span xtd::threading::timeout::infinite_time_span
static

A constant used to specify an infinite waiting period, for methods that accept a TimeSpan parameter.

Remarks
For threading methods that accept a timeout parameter of type TimeSpan, such as Thread.Sleep(TimeSpan) and Thread.Join(TimeSpan), this value is used to suspend the thread indefinitely. However, in most cases, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to sychronize threads or manage resources.
The value of this field is -00:00:00.0010000, or -1 millisecond.

The documentation for this class was generated from the following file: