A synchronization primitive that can also be used for interprocess synchronization.
milliseconds_timeoutor timeout parameter has elapsed. When this happens, the xtd::threading::wait_handle::wait_one method returns false, and the calling thread makes no further attempt to acquire ownership of the mutex. In this case, you should structure your code so that access to the resource that is protected by the mutex is denied to the calling thread. Because the thread never acquired ownership of the mutex, it must not call the xtd::threading::mutex::release_mutex method. The second example in the Examples section illustrates this pattern.
Public Aliases | |
using | native_handle_type = intptr |
Rpresents the native handle type. | |
Public Constructors | |
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. | |
Public Properties | |
intptr | handle () const noexcept override |
Gets the native operating system handle. | |
void | handle (intptr value) override |
Sets the native operating system handle. | |
native_handle_type | native_handle () const noexcept |
Returns the underlying implementation-defined native handle object. | |
Public Methods | |
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. | |
Public Static Methods | |
static mutex | open_existing (const string &name) |
Opens an existing named mutex. | |
static bool | try_open_existing (const string &name, mutex &result) noexcept |
Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded. | |
Protected Methods | |
bool | signal () override |
Releases ownership of the specified wait_handle object. | |
bool | wait (int32 milliseconds_timeout) override |
wait ownership of the specified mutex object. | |
Additional Inherited Members | |
![]() | |
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. | |
![]() | |
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 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. | |
![]() | |
![]() | |
abstract_object ()=default | |
Initializes a new instance of the xtd::abstract_object class. | |
Rpresents the native handle type.
xtd::threading::mutex::mutex | ( | ) |
Initializes a new instance of the xtd::threading::mutex class with default properties.
|
explicit |
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.
initially_owned | true to give the calling thread initial ownership of the named system mutex if the named system mutex is created as a result of this call; otherwise, false. |
|
explicit |
Initializes a new instance of the xtd::threading::mutex class with a string that is the name of the mutex.
name | The name, if the synchronization object is to be shared with other processes; otherwise, an empty string. The name is case-sensitive. The backslash character () and slsh (/) are reserved. |
xtd::io::io_xception | name is invalid. This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. Note that the name and common prefixes "Global\" and "Local" are case-sensitive. -or- There was some other error. The HResult property may provide more information. |
xtd::threading::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.
name | The name, if the synchronization object is to be shared with other processes; otherwise, an empty string. The name is case-sensitive. The backslash character () and slsh (/) are reserved. |
created_new | When this method returns, contains a bool that is true if a local mutex was created (that is, if name is empty string) or if the specified named system mutex was created; false if the specified named system mutex already existed. |
xtd::io::io_xception | name is invalid. This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. Note that the name and common prefixes "Global\" and "Local" are case-sensitive. -or- There was some other error. The HResult property may provide more information. |
xtd::threading::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.
initially_owned | true to give the calling thread initial ownership of the named system mutex if the named system mutex is created as a result of this call; otherwise, false. |
name | The name, if the synchronization object is to be shared with other processes; otherwise, an empty string. The name is case-sensitive. The backslash character () and slsh (/) are reserved. |
xtd::io::io_xception | name is invalid. This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. Note that the name and common prefixes "Global\" and "Local" are case-sensitive. -or- There was some other error. The HResult property may provide more information. |
xtd::threading::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.
initially_owned | true to give the calling thread initial ownership of the named system mutex if the named system mutex is created as a result of this call; otherwise, false. |
name | The name, if the synchronization object is to be shared with other processes; otherwise, an empty string. The name is case-sensitive. The backslash character () and slsh (/) are reserved. |
created_new | When this method returns, contains a bool that is true if a local mutex was created (that is, if name is empty string) or if the specified named system mutex was created; false if the specified named system mutex already existed. |
xtd::io::io_xception | name is invalid. This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. Note that the name and common prefixes "Global\" and "Local" are case-sensitive. -or- There was some other error. The HResult property may provide more information. |
|
overridevirtualnoexcept |
Gets the native operating system handle.
Implements xtd::threading::wait_handle.
|
overridevirtual |
Sets the native operating system handle.
value | An intptr representing the native operating system handle. |
Implements xtd::threading::wait_handle.
|
noexcept |
Returns the underlying implementation-defined native handle object.
|
overridevirtual |
Releases all resources held by the current xtd::threading::wait_handle.
Reimplemented from xtd::threading::wait_handle.
void xtd::threading::mutex::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 xtd::threading::mutex::release_mutex | ( | ) |
Releases the Mutex once.
xtd::object_closed_exception | the handle is invalid |
|
noexcept |
Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true, otherwise returns false.
timeout | timeout A xtd::time_span that represents the number of milliseconds to wait, or a xtd::time_span that represents -1 milliseconds to wait indefinitely. |
|
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.
timeout | minimum duration to block for |
|
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.
timeout_time | maximum time point to block until |
void xtd::threading::mutex::unlock | ( | ) |
Unlocks the mutex.
Opens an existing named mutex.
name | The name of a system-wide named mutex object. |
xtd::argument_exception | is a zero-length string -or- name is longer than 128 characters |
xtd::io::io_exception | An Io error occurred. |
|
staticnoexcept |
Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.
name | The name of the synchronization object to be shared with other processes. The name is case-sensitive. The backslash character () and (/) are reserved. |
result | When this method returns, contains a xtd::threading::mutex object that represents the named mutex if the call succeeded. |
|
overrideprotectedvirtual |
Releases ownership of the specified wait_handle object.
Implements xtd::threading::wait_handle.
|
overrideprotectedvirtual |
wait ownership of the specified mutex object.
handle | A valid handle to an open object. |
milliseconds_timeout | The number of milliseconds to wait, or -1 to wait indefinitely. |
Implements xtd::threading::wait_handle.
|
virtualnoexcept |
Determines whether the specified object is equal to the current object.
obj | The object to compare with the current object. |
Reimplemented from xtd::object.
|
inlinestaticnoexcept |
Determines whether the specified object instances are considered equal.
object_a | The first object to compare. |
object_b | The second object to compare. |