A synchronization primitive that can also be used for interprocess synchronization. 
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:21
 
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
 
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.hpp:50
 
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.hpp:52
 
  - Header
 #include <xtd/threading/mutex>
  
- Namespace
 - xtd::threading 
 
- Library
 - xtd.core
 
- Warning
 - An abandoned mutex often indicates a serious error in the code. When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. The next thread to request ownership of the mutex can handle this exception and proceed, if the integrity of the data structures can be verified. 
 
- Warning
 - The backslash () and slash(/) are a reserved character in a mutex name. Don't use a backslash () and slash (/) in a mutex name except as specified in the note on using mutexes in terminal server sessions. Otherwise, a xtd::io::directory_not_found_exception may be thrown, even though the name of the mutex represents an existing file. 
 
- Examples
 - This example shows how a local xtd::threading::mutex object is used to synchronize access to a protected resource. Because each calling thread is blocked until it acquires ownership of the mutex, it must call the xtd::threading::mutex::release_mutex method to release ownership of the mutex. 
#include <xtd/collections/generic/list>
#include <xtd/threading/mutex>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
 
 
namespace mutex_example {
  class program {
  public:
    static void main() {
      
      for (
auto i = 0; 
i < num_threads; ++
i) {
 
        threads.emplace_back(thread_proc);
        threads.back().name(string::format("thread_{0}", i + 1));
        threads.back().start();
      }
      
      thread::join_all(threads);
    }
    
    static void thread_proc() {
      for(
auto i = 0; 
i < num_iterations; ++
i)
 
        Use_resource();
    }
    
  private:
    
    
    static void Use_resource() {
      
      console::write_line("{0} is requesting the mutex",
                        thread::current_thread().name());
      mut.wait_one();
      
      console::write_line("{0} has entered the protected area",
                          thread::current_thread().name());
      
      
      
      
      thread::sleep(500);
      
      console::write_line("{0} is leaving the protected area",
                          thread::current_thread().name());
      
      
      mut.release_mutex();
      console::write_line("{0} has released the mutex",
                          thread::current_thread().name());
    }
 
    
    inline static constexpr int num_iterations = 1;
    inline static constexpr int num_threads = 3;
  };
}
 
 
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
 
#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::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
 
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
 
   
- Examples
 - mutex.cpp.
 
 | 
|   | mutex () | 
|   | Initializes a new instance of the xtd::threading::mutex class with default properties.  
  | 
|   | 
|   | mutex (bool initially_owned) | 
|   | Initializes a new instance of the xtd::threading::mutex with a bool value that indicates whether the calling thread should have initial ownership of the mutex.  
  | 
|   | 
|   | mutex (const string &name) | 
|   | Initializes a new instance of the xtd::threading::mutex class with a string that is the name of the mutex.  
  | 
|   | 
|   | mutex (const string &name, bool &created_new) | 
|   | Initializes a new instance of the xtd::threading::mutex class with a string that is the name of the mutex, and a bool value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.  
  | 
|   | 
|   | mutex (bool initially_owned, const string &name) | 
|   | Initializes a new instance of the xtd::threading::mutex class with a bool value that indicates whether the calling thread should have initial ownership of the mutex, and a string that is the name of the mutex.  
  | 
|   | 
|   | mutex (bool initially_owned, const string &name, bool &created_new) | 
|   | Initializes a new instance of the xtd::threading::mutex class with a bool value that indicates whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, and a bool value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.  
  | 
|   | 
 | 
| void  | close () override | 
|   | Releases all resources held by the current xtd::threading::wait_handle.  
  | 
|   | 
| 
int32  | compare_to (const mutex &value) const noexcept override | 
|   | 
| 
bool  | equals (const mutex &value) const noexcept override | 
|   | 
| void  | lock () | 
|   | Locks the mutex. If another thread has already locked the mutex, a call to lock will block execution until the lock is acquired.  
  | 
|   | 
| void  | release_mutex () | 
|   | Releases the Mutex once.  
  | 
|   | 
| bool  | try_lock () noexcept | 
|   | Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true, otherwise returns false.  
  | 
|   | 
| bool  | try_lock_for (const time_span &timeout) noexcept | 
|   | Tries to lock the mutex. Blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. On successful lock acquisition returns true, otherwise returns false.  
  | 
|   | 
| bool  | try_lock_until (const date_time &timeout_time) noexcept | 
|   | Tries to lock the mutex. Blocks until specified timeout_time has been reached or the lock is acquired, whichever comes first. On successful lock acquisition returns true, otherwise returns false.  
  | 
|   | 
| void  | unlock () | 
|   | Unlocks the mutex.  
  | 
|   | 
| virtual bool  | equals (const object &obj) const noexcept | 
|   | Determines whether the specified object is equal to the current object.  
  | 
|   | 
| template<typename object_a_t , typename object_b_t >  | 
| static bool  | equals (const object_a_t &object_a, const object_b_t &object_b) noexcept | 
|   | Determines whether the specified object instances are considered equal.  
  | 
|   | 
 | 
  Static Public Attributes inherited from xtd::threading::wait_handle | 
| static const intptr  | invalid_handle | 
|   | Represents an invalid native operating system handle. This field is read-only.  
  | 
|   | 
| static constexpr size_t  | wait_timeout | 
|   | Indicates that a xtd::threading::wait_handle::wait_any operation timed out before any of the wait handles were signaled. This field is constant.  
  | 
|   | 
  Public Member Functions inherited from xtd::threading::wait_handle | 
|   | wait_handle ()=default | 
|   | Initializes a new instance of the xtd::threading::wait_handle class.  
  | 
|   | 
| virtual bool  | wait_one () | 
|   | Blocks the current thread until the current xtd::threading::wait_handle receives a signal.  
  | 
|   | 
| virtual bool  | wait_one (int32 milliseconds_timeout) | 
|   | Blocks the current thread until the current xtd::threading::wait_handle receives a signal, using 32-bit signed integer to measure the time interval.  
  | 
|   | 
| virtual bool  | wait_one (const time_span &timeout) | 
|   | Blocks the current thread until the current instance receives a signal, using a xtd::time_span to measure the time interval.  
  | 
|   | 
|   | object ()=default | 
|   | Create a new instance of the ultimate base class object.  
  | 
|   | 
| virtual size_t  | get_hash_code () const noexcept | 
|   | Serves as a hash function for a particular type.  
  | 
|   | 
| virtual type_object  | get_type () const noexcept | 
|   | Gets the type of the current instance.  
  | 
|   | 
| template<typename object_t >  | 
| xtd::uptr< object_t >  | memberwise_clone () const | 
|   | Creates a shallow copy of the current object.  
  | 
|   | 
| virtual xtd::string  | to_string () const noexcept | 
|   | Returns a xtd::string that represents the current object.  
  | 
|   | 
| virtual int32  | compare_to (const mutex &obj) const noexcept=0 | 
|   | Compares the current instance with another object of the same type.  
  | 
|   | 
| virtual bool  | equals (const mutex &) const noexcept=0 | 
|   | Indicates whether the current object is equal to another object of the same type.  
  | 
|   | 
  Static Public Member Functions inherited from xtd::threading::wait_handle | 
| static bool  | signal_and_wait (wait_handle &to_signal, wait_handle &to_wait) | 
|   | Signals one xtd::threading::wait_handle and waits on another.  
  | 
|   | 
| static bool  | signal_and_wait (wait_handle &to_signal, wait_handle &to_wait, int32 milliseconds_timeout) | 
|   | Signals one xtd::threading::wait_handle and waits on another, specifying a time-out interval as a 32-bit signed integer.  
  | 
|   | 
| static bool  | signal_and_wait (wait_handle &to_signal, wait_handle &to_wait, const time_span &timeout) | 
|   | Signals one xtd::threading::wait_handle and waits on another, specifying a time-out interval as a time_span.  
  | 
|   | 
| template<typename collection_t >  | 
| static bool  | wait_all (const collection_t &wait_handles) | 
|   | Waits for all the elements in the specified collection to receive a signal.  
  | 
|   | 
| template<typename collection_t >  | 
| static bool  | wait_all (const collection_t &wait_handles, int32 milliseconds_timeout) | 
|   | Waits for all the elements in the specified collection to receive a signal, using an int32 value to measure the time interval.  
  | 
|   | 
| template<typename collection_t >  | 
| static bool  | wait_all (const collection_t &wait_handles, const time_span &timeout) | 
|   | Waits for all the elements in the specified collection to receive a signal, using a xtd::time_span value to measure the time interval.  
  | 
|   | 
| template<typename collection_t >  | 
| static size_t  | wait_any (const collection_t &wait_handles) | 
|   | Waits for any of the elements in the specified collection to receive a signal.  
  | 
|   | 
| template<typename collection_t >  | 
| static size_t  | wait_any (const collection_t &wait_handles, int32 milliseconds_timeout) | 
|   | Waits for any of the elements in the specified collection to receive a signal, using a 32-bit signed integer to measure the time interval.  
  | 
|   | 
| template<typename collection_t >  | 
| static size_t  | wait_any (const collection_t &wait_handles, const time_span &timeout) | 
|   | Waits for any of the elements in the specified collection to receive a signal, using a xtd::time_span to measure the time interval.  
  | 
|   | 
| template<typename object_a_t , typename object_b_t >  | 
| static bool  | equals (const object_a_t &object_a, const object_b_t &object_b) noexcept | 
|   | Determines whether the specified object instances are considered equal.  
  | 
|   | 
| template<typename object_a_t , typename object_b_t >  | 
| static bool  | reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept | 
|   | Determines whether the specified object instances are the same instance.  
  | 
|   | 
  Protected Member Functions inherited from xtd::threading::wait_handle | 
|   | abstract_object ()=default | 
|   | Initializes a new instance of the xtd::abstract_object class.  
  | 
|   |