Provides atomic operations for variables that are shared by multiple threads. 
Provides atomic operations for variables that are shared by multiple threads.
Definition interlocked.hpp:40
 
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
 
#define core_export_
Define shared library export.
Definition core_export.hpp:13
 
  - Inheritance
 - xtd::static_object → xtd::threading::interlocked 
 
- Header
 #include <xtd/threading/interlocked>
  
- Namespace
 - xtd::threading 
 
- Library
 - xtd.core
 
- Examples
 - The following code example shows a thread-safe resource locking mechanism. 
#include <xtd/threading/interlocked>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/random>
#include <xtd/startup>
 
 
namespace interlocked_example {
  struct my_thread {
    string name;
  };
  
  class my_interlocked_exchange_class {
  private:
    
    inline static int using_resource = 0;
    
    static constexpr int num_thread_iterations = 5;
    
  public:
    
    static auto main() {
      auto my_threads = std::array<my_thread, 10> {};
      
      for (auto index = 0ul; index < my_threads.size(); ++index) {
        my_threads[index].name = string::format("Thread{}", index + 1);
        
        
        thread::sleep(rnd.next(0, 1000));
        my_threads[index].thread = 
thread {my_thread_proc};
 
        my_threads[index].
thread.start(my_threads[index].name);
 
      }
      
      for (auto index = 0ul; index < my_threads.size(); ++index)
    }
    
    static void my_thread_proc(std::any name) {
      for (auto index = 0; index < num_thread_iterations; ++index) {
        use_resource(as<string>(name));
        
        
        thread::sleep(1000);
      }
    }
    
    
    static auto use_resource(const string& name)->bool {
      
      if (0 == interlocked::exchange(using_resource, 1)) {
        console::write_line("{} acquired the lock", name);
        
        
        
        
        thread::sleep(500);
        
        console::write_line("{} exiting lock", name);
        
        
        interlocked::exchange(using_resource, 0);
        return true;
      } else {
        console::write_line("   {} was denied the lock", name);
        return false;
      }
    }
  };
}
 
startup_(interlocked_example::my_interlocked_exchange_class::main);
 
 
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:40
 
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:43
 
thread(const xtd::threading::parameterized_thread_start &start)
Initializes a new instance of the xtd::threading::thread class, specifying a delegate that allows an ...
 
void join()
Blocks the calling thread until this thread object terminates, while continuing to perform standard C...
 
#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:175
 
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:11
 
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
 
   
 | 
| static int32  | add (int32 &location, int32 value) noexcept | 
|   | Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation.  
  | 
|   | 
| static int64  | add (int64 &location, int64 value) noexcept | 
|   | Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation.  
  | 
|   | 
| static double  | compare_exchange (double &location, double value, double comparand) noexcept | 
|   | Compares two Double for equality and, if they are equal, replaces one of the values.  
  | 
|   | 
| static int32  | compare_exchange (int32 &location, int32 value, int32 comparand) noexcept | 
|   | Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values.  
  | 
|   | 
| static int64  | compare_exchange (int64 &location, int64 value, int64 comparand) noexcept | 
|   | Compares two 64-bit signed integers for equality and, if they are equal, replaces one of the values.  
  | 
|   | 
| static void *  | compare_exchange (void *&location, void *value, void *comparand) noexcept | 
|   | Compares two platform-specific handles or pointers for equality and, if they are equal, replaces one of them.  
  | 
|   | 
| template<typename type_t >  | 
| static type_t  | compare_exchange (object &location, const object &value, const object &comparand) noexcept | 
|   | Compares two objects for equality and, if they are equal, replaces one of them.  
  | 
|   | 
| template<typename type_t >  | 
| static type_t  | compare_exchange (type_t &location, type_t value, type_t comparand) noexcept | 
|   | Compares two instances of the specified reference type type_t for equality and, if they are equal, replaces one of them.  
  | 
|   | 
| static float  | compare_exchange (float &location, float value, float comparand) noexcept | 
|   | Compares two Single for equality and, if they are equal, replaces one of the values.  
  | 
|   | 
| static int32  | decrement (int32 &location) noexcept | 
|   | Decrements a specified variable and stores the result, as an atomic operation.  
  | 
|   | 
| static int64  | decrement (int64 &location) noexcept | 
|   | Decrements a specified variable and stores the result, as an atomic operation.  
  | 
|   | 
| template<typename type_t >  | 
| static type_t  | exchange (type_t &location, type_t value) | 
|   | Sets a variable of the specified type type_t to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static double  | exchange (double &location, double value) noexcept | 
|   | Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static int32  | exchange (int32 &location, int32 value) noexcept | 
|   | Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static int64  | exchange (int64 &location, int64 value) noexcept | 
|   | Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static void *  | exchange (void *&location, void *value) noexcept | 
|   | Sets a platform-specific handles or pointers to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| template<typename type_t >  | 
| static type_t  | exchange (object &location, const object &value) noexcept | 
|   | Sets an object to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static float  | exchange (float &location, float value) noexcept | 
|   | Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.  
  | 
|   | 
| static int32  | increment (int32 &location) noexcept | 
|   | Increments a specified variable and stores the result, as an atomic operation.  
  | 
|   | 
| static int64  | increment (int64 &location) noexcept | 
|   | Increments a specified variable and stores the result, as an atomic operation.  
  | 
|   | 
| static void  | memory_barrier () noexcept | 
|   | Synchronizes memory access as follows: The processor that executes the current thread cannot reorder instructions in such a way that memory accesses before the call to xtd::threading::interlocked::memory_barrier execute after memory accesses that follow the call to xtd::threading::interlocked::memory_barrier.  
  | 
|   | 
| static int64  | read (int64 &location) noexcept | 
|   | Returns a 64-bit value, loaded as an atomic operation.  
  | 
|   |