xtd 0.2.0
Loading...
Searching...
No Matches
threading

Definition

Contains threading definitions.

Modules

 tasks
 Contains tasks definitions.
 

Classes

class  xtd::threading::auto_reset_event
 Represents a thread synchronization event that, when signaled, resets automatically after releasing a single waiting thread. This class cannot be inherited. More...
 
class  xtd::threading::barrier
 Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases. More...
 
class  xtd::threading::cancellation_token
 Propagates notification that operations should be canceled. More...
 
class  xtd::threading::cancellation_token_registration
 Propagates notification that operations should be canceled. More...
 
class  xtd::threading::cancellation_token_source
 Signals to a xtd::threading::cancellation_token that it should be canceled. More...
 
class  xtd::threading::countdown_event
 Represents a synchronization primitive that is signaled when its count reaches zero. More...
 
class  xtd::threading::event_wait_handle
 Represents a thread synchronization event. More...
 
class  xtd::threading::interlocked
 Provides atomic operations for variables that are shared by multiple threads. More...
 
class  xtd::threading::jthread
 Creates and controls an automatically rejoins on destruction thread, sets its priority, and gets its status. More...
 
class  xtd::threading::lock_guard
 Provides a mechanism that synchronizes access to objects with xtd::threading::monitor. More...
 
class  xtd::threading::manual_reset_event
 Represents a thread synchronization event that, when signaled, must be reset manually. This class cannot be inherited. More...
 
class  xtd::threading::monitor
 Provides a mechanism that synchronizes access to objects. More...
 
class  xtd::threading::mutex
 A synchronization primitive that can also be used for interprocess synchronization. More...
 
class  xtd::threading::registered_wait_handle
 A synchronization primitive that can also be used for interprocess synchronization. More...
 
class  xtd::threading::semaphore
 Limits the number of threads that can access a resource or pool of resources concurrently. More...
 
class  xtd::threading::spin_lock
 Provides a mutual exclusion lock primitive where a thread trying to acquire the lock waits in a loop repeatedly checking until the lock becomes available. More...
 
class  xtd::threading::thread
 Creates and controls a thread, sets its priority, and gets its status. More...
 
class  xtd::threading::thread_local_object< value_t >
 Provides thread-local storage of data. More...
 
class  xtd::threading::thread_pool
 Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers. More...
 
class  xtd::threading::timeout
 Contains a constant used to specify an infinite amount of time. This class cannot be inherited. More...
 
class  xtd::threading::timer
 Provides a mechanism for executing a method on a thread pool thread at specified intervals. This class cannot be inherited. More...
 
class  xtd::threading::wait_handle
 Encapsulates operating system specific objects that wait for exclusive access to shared resources. More...
 

Typedefs

using xtd::lock = xtd::threading::lock_guard
 Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
 

Enumerations

enum class  xtd::threading::event_reset_mode {
  xtd::threading::event_reset_mode::auto_reset ,
  xtd::threading::event_reset_mode::manual_reset
}
 Indicates whether an xtd::threading::event_wait_handle is reset automatically or manually after receiving a signal. More...
 
enum class  xtd::threading::thread_priority {
  xtd::threading::thread_priority::lowest ,
  xtd::threading::thread_priority::below_normal ,
  xtd::threading::thread_priority::normal ,
  xtd::threading::thread_priority::above_normal ,
  xtd::threading::thread_priority::highest
}
 Specifies the scheduling priority of a system::threading::thread. More...
 
enum class  xtd::threading::thread_state {
  xtd::threading::thread_state::running ,
  xtd::threading::thread_state::stop_requested ,
  xtd::threading::thread_state::suspend_requested ,
  xtd::threading::thread_state::background ,
  xtd::threading::thread_state::unstarted ,
  xtd::threading::thread_state::stopped ,
  xtd::threading::thread_state::wait_sleep_join ,
  xtd::threading::thread_state::suspended ,
  xtd::threading::thread_state::abort_requested ,
  xtd::threading::thread_state::aborted
}
 Specifies the execution states of a System::Threading::Thread. More...
 

Typedef Documentation

◆ lock

#include <xtd.core/include/xtd/lock.h>

Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.

Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
Namespace
xtd
Library
xtd.core
Examples
The following example uses the xtd::lock class to synchronize access to a single instance of a random number generator represented by the xtd::random class. The example creates ten threads, each of which executes asynchronously on a thread pool thread. Each thread generates 10,000 random numbers, calculates their average, and updates two procedure-level variables that maintain a running total of the number of random numbers generated and their sum. After all threads have executed, these two values are then used to calculate the overall mean.
#include <xtd/threading/thread_pool>
#include <xtd/console>
#include <xtd/lock>
#include <xtd/random>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace examples {
class account : public object {
public:
explicit account(int initial) : balance(initial) {}
void do_transactions() {
for (auto i = 0; i < 100; ++i)
withdraw(random.next(1, 100));
}
private:
int withdraw(int amount) {
// This condition never is true unless the lock statement is commented out.
if (balance < 0) throw system_exception {"Negative Balance"};
// Comment out the next line to see the effect of leaving out the lock keyword.
lock_(balance) {
if (balance < amount) return 0;
console::write_line("Balance before Withdrawal : {0}", balance);
console::write_line("Amount to Withdraw : -{0}", amount);
balance = balance - amount;
console::write_line("Balance after Withdrawal : {0}", balance);
return amount;
}
return 0;
}
int balance = 0;
};
class program {
public:
static void main() {
auto account = examples::account {1000};
for (auto i = 0; i < 10; ++i)
thread_pool::queue_user_work_item({account, &account::do_transactions});
thread_pool::close();
}
};
}
startup_(examples::program::main);
// This code can produce the following output :
//
// Balance before Withdrawal : 1000
// Amount to Withdraw : -75
// Balance after Withdrawal : 925
// Balance before Withdrawal : 925
// Amount to Withdraw : -84
// Balance after Withdrawal : 841
// Balance before Withdrawal : 841
// Amount to Withdraw : -69
// Balance after Withdrawal : 772
// Balance before Withdrawal : 772
// Amount to Withdraw : -71
// Balance after Withdrawal : 701
// Balance before Withdrawal : 701
// Amount to Withdraw : -84
// Balance after Withdrawal : 617
// Balance before Withdrawal : 617
// Amount to Withdraw : -81
// Balance after Withdrawal : 536
// Balance before Withdrawal : 536
// Amount to Withdraw : -15
// Balance after Withdrawal : 521
// Balance before Withdrawal : 521
// Amount to Withdraw : -93
// Balance after Withdrawal : 428
// Balance before Withdrawal : 428
// Amount to Withdraw : -6
// Balance after Withdrawal : 422
// Balance before Withdrawal : 422
// Amount to Withdraw : -54
// Balance after Withdrawal : 368
// Balance before Withdrawal : 368
// Amount to Withdraw : -52
// Balance after Withdrawal : 316
// Balance before Withdrawal : 316
// Amount to Withdraw : -67
// Balance after Withdrawal : 249
// Balance before Withdrawal : 249
// Amount to Withdraw : -93
// Balance after Withdrawal : 156
// Balance before Withdrawal : 156
// Amount to Withdraw : -60
// Balance after Withdrawal : 96
// Balance before Withdrawal : 96
// Amount to Withdraw : -7
// Balance after Withdrawal : 89
// Balance before Withdrawal : 89
// Amount to Withdraw : -42
// Balance after Withdrawal : 47
// Balance before Withdrawal : 47
// Amount to Withdraw : -1
// Balance after Withdrawal : 46
// Balance before Withdrawal : 46
// Amount to Withdraw : -22
// Balance after Withdrawal : 24
// Balance before Withdrawal : 24
// Amount to Withdraw : -18
// Balance after Withdrawal : 6
// Balance before Withdrawal : 6
// Amount to Withdraw : -2
// Balance after Withdrawal : 4
// Balance before Withdrawal : 4
// Amount to Withdraw : -2
// Balance after Withdrawal : 2
// Balance before Withdrawal : 2
// Amount to Withdraw : -2
// Balance after Withdrawal : 0
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.h:40
virtual int32 next() const
Returns a nonnegative random number.
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
#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:175
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.h:85
@ i
The I key.
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
See xtd::threading::monitor for more information.

Enumeration Type Documentation

◆ event_reset_mode

#include <xtd.core/include/xtd/threading/event_reset_mode.h>

Indicates whether an xtd::threading::event_wait_handle is reset automatically or manually after receiving a signal.

enum class event_reset_mode
event_reset_mode
Indicates whether an xtd::threading::event_wait_handle is reset automatically or manually after recei...
Definition event_reset_mode.h:24
Header
#include <xtd/threading/event_reset_mode>
Namespace
xtd::threading
Library
xtd.core
Enumerator
auto_reset 

When signaled, the xtd::threading::event_wait_handle resets automatically after releasing a single thread. If no threads are waiting, the xtd::threading::event_wait_handle remains signaled until a thread blocks, and resets after releasing the thread.

manual_reset 

When signaled, the xtd::threading::event_wait_handle releases all waiting threads and remains signaled until it is manually reset.

◆ thread_priority

#include <xtd.core/include/xtd/threading/thread_priority.h>

Specifies the scheduling priority of a system::threading::thread.

enum class thread_priority
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition thread_priority.h:24
Header
#include <xtd/threading/thread_priority>
Namespace
xtd::threading
Library
xtd.core
Enumerator
lowest 

The system::threading::thread can be scheduled after threads with any other priority.

below_normal 

The system::threading::thread can be scheduled after threads with thread_priority::normal priority and before those with thread_priority::lowest priority.

normal 

The system::threading::thread can be scheduled after threads with thread_priority::above_normal priority and before those with thread_priority::below_normal priority. Threads have thread_priority::normal priority by default.

above_normal 

The system::threading::thread can be scheduled after threads with thread_priority::highest priority and before those with thread_priority::normal priority.

highest 

The system::threading::thread can be scheduled before threads with any other priority.

◆ thread_state

enum class xtd::threading::thread_state
strong

#include <xtd.core/include/xtd/threading/thread_state.h>

Specifies the execution states of a System::Threading::Thread.

enum class thread_state
thread_state
Specifies the execution states of a System::Threading::Thread.
Definition thread_state.h:24
Header
#include <xtd/threading/thread_state>
Namespace
xtd::threading
Library
xtd.core
Enumerator
running 

The thread_state has been started, it is not blocked, and there is no pending System::Threading::ThreadAbortException.

stop_requested 

The thread_state is being requested to stop. This is for internal use only.

suspend_requested 

The thread_state is being requested to suspend.

background 

The thread_state is being executed as a background thread_state, as opposed to a foreground thread_state. This state is controlled by setting the System::Threading::Thread.Isbackground property.

unstarted 

The System::Threading::Thread.Start() method has not been invoked on the thread_state.

stopped 

The thread_state has stopped.

wait_sleep_join 

The thread_state is blocked. This could be the result of calling System::Threading::Thread.Sleep(System::Int32) or System::Threading::Thread.Join(), of requesting a lock � for example, by calling System::Threading::Monitor.Enter(System::object) or System::Threading::Monitor.Wait(System::object,System::Int32,System::Boolean) or of waiting on a thread_state synchronization object such as System::Threading::ManualResetEvent.

suspended 

The thread_state has been suspended.

abort_requested 

The System::Threading::Thread.Abort(System::object) method has been invoked on the thread_state, but the thread_state has not yet received the pending System::Threading::ThreadAbortException that will attempt to terminate it.

aborted 

The thread_state state includes System::Threading::ThreadState.abort_requested and the thread_state is now dead, but its state has not yet changed to System::Threading::ThreadState.stopped.