A synchronization primitive that can also be used for interprocess synchronization.
- 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/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(ustring::format("thread_{0}", i + 1));
threads.back().start();
}
}
static void thread_proc() {
for(
auto i = 0;
i < num_iterations; ++
i)
Use_resource();
}
private:
static void Use_resource() {
mut.wait_one();
mut.release_mutex();
}
inline static std::vector<thread> threads;
inline static constexpr int num_iterations = 1;
inline static constexpr int num_threads = 3;
};
}
- Examples:
- mutex.cpp.
|
| mutex () |
| Initializes a new instance of the xtd::threading::mutex class with default properties. More...
|
|
| 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. More...
|
|
| mutex (const ustring &name) |
| Initializes a new instance of the xtd::threading::mutex class with a string that is the name of the mutex. More...
|
|
| mutex (const ustring &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. More...
|
|
| mutex (bool initially_owned, const ustring &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. More...
|
|
| mutex (bool initially_owned, const ustring &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. More...
|
|
|
void | close () override |
| Releases all resources held by the current xtd::threading::wait_handle. More...
|
|
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. More...
|
|
void | release_mutex () |
| Releases the Mutex once. More...
|
|
bool | try_lock () noexcept |
| Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true, otherwise returns false. More...
|
|
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. More...
|
|
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. More...
|
|
void | unlock () |
| Unlocks the mutex. More...
|
|
static mutex | open_existing (const ustring &name) |
| Opens an existing named mutex. More...
|
|
static bool | try_open_existing (const ustring &name, mutex &result) noexcept |
| Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded. More...
|
|
|
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. More...
|
|
static constexpr size_t | wait_timeout = std::numeric_limits<size_t>::max() |
| Indicates that a xtd::threading::wait_handle::wait_any operation timed out before any of the wait handles were signaled. This field is constant. This field is one of the possible return values of xtd::threading::wait_handle::wait_any. More...
|
|
Public Member Functions inherited from xtd::threading::wait_handle |
| wait_handle ()=default |
| Initializes a new instance of the xtd::threading::wait_handle class. More...
|
|
virtual bool | wait_one () |
| Blocks the current thread until the current xtd::threading::wait_handle receives a signal. More...
|
|
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. More...
|
|
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. More...
|
|
| object ()=default |
| Create a new instance of the ultimate base class object. More...
|
|
bool | equals (const object &obj) const noexcept |
| Determines whether the specified object is equal to the current object. More...
|
|
virtual size_t | get_hash_code () const noexcept |
| Serves as a hash function for a particular type. More...
|
|
virtual type_object | get_type () const noexcept |
| Gets the type of the current instance. More...
|
|
template<typename object_t > |
std::unique_ptr< object_t > | memberwise_clone () const noexcept |
| Creates a shallow copy of the current object. More...
|
|
virtual xtd::ustring | to_string () const noexcept |
| Returns a sxd::ustring that represents the current object. More...
|
|
virtual int32 | compare_to (const mutex &obj) const noexcept=0 |
| Compares the current instance with another object of the same type. More...
|
|
virtual bool | equals (const mutex &) const noexcept=0 |
| Indicates whether the current object is equal to another object of the same type. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
static bool | equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are considered equal. More...
|
|
static bool | reference_equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are the same instance. More...
|
|
Protected Member Functions inherited from xtd::threading::wait_handle |
| abstract_object ()=default |
| Initializes a new instance of the xtd::abstract_object class. More...
|
|