xtd 0.2.0
Loading...
Searching...
No Matches
mutex.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "wait_handle.hpp"
6#include "../date_time.hpp"
7#include "../icomparable.hpp"
8#include "../iequatable.hpp"
9
11namespace xtd {
13 namespace threading {
15 class thread;
17
50 class core_export_ mutex : public wait_handle, public icomparable<mutex>, public iequatable<mutex> {
51 class mutex_base;
52 class named_mutex;
53 class unnamed_mutex;
54 public:
56
61
63
70 explicit mutex(bool initially_owned);
75 explicit mutex(const string& name);
81 mutex(const string& name, bool& created_new);
86 mutex(bool initially_owned, const string& name);
92 mutex(bool initially_owned, const string& name, bool& created_new);
94
96 template<class char_t>
97 explicit mutex(const char_t* name) : mutex(string(name)) {}
98 ~mutex();
100
102
104 [[nodiscard]] auto handle() const noexcept -> intptr override;
105 auto handle(intptr value) -> void override;
106
109 auto native_handle() const noexcept -> native_handle_type;
111
113
115 auto close() -> void override;
116
117 [[nodiscard]] auto compare_to(const mutex& value) const noexcept -> int32 override;
118
119 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override;
120 [[nodiscard]] auto equals(const mutex& other) const noexcept -> bool override;
121
126 auto lock() -> void;
127
130 auto release_mutex() -> void;
131
138 [[nodiscard]] auto try_lock() noexcept -> bool;
139
149 [[nodiscard]] auto try_lock_for(const time_span& timeout) noexcept -> bool;
150
160 [[nodiscard]] auto try_lock_until(const date_time& timeout_time) noexcept -> bool;
161
166 auto unlock() -> void;
168
170
177 [[nodiscard]] static auto open_existing(const string& name) -> mutex;
178
183 [[nodiscard]] static auto try_open_existing(const string& name, mutex& result) noexcept -> bool;
185
186 protected:
188
190 auto signal() -> bool override;
191
192 auto wait(int32 milliseconds_timeout) -> bool override;
194
195 private:
196 friend class thread;
197 auto create(bool initially_owned, bool& created_new) -> void;
198 xtd::sptr<mutex_base> mutex_;
199 string name_;
200 };
201 }
202}
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:83
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:22
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.hpp:50
static auto try_open_existing(const string &name, mutex &result) noexcept -> bool
Opens the specified named mutex, if it already exists, and returns a value that indicates whether the...
auto native_handle() const noexcept -> native_handle_type
Returns the underlying implementation-defined native handle object.
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 m...
mutex(bool initially_owned, const string &name)
Initializes a new instance of the xtd::threading::mutex class with a bool value that indicates whethe...
auto try_lock_until(const date_time &timeout_time) noexcept -> bool
Tries to lock the mutex. Blocks until specified timeout_time has been reached or the lock is acquired...
auto handle() const noexcept -> intptr override
Gets the native operating system handle.
auto lock() -> void
Locks the mutex. If another thread has already locked the mutex, a call to lock will block execution ...
auto try_lock() noexcept -> bool
Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true,...
intptr native_handle_type
Rpresents the native handle type.
Definition mutex.hpp:59
mutex(bool initially_owned)
Initializes a new instance of the xtd::threading::mutex with a bool value that indicates whether the ...
auto try_lock_for(const time_span &timeout) noexcept -> bool
Tries to lock the mutex. Blocks until specified timeout_duration has elapsed or the lock is acquired,...
static auto open_existing(const string &name) -> mutex
Opens an existing named mutex.
mutex()
Initializes a new instance of the xtd::threading::mutex class with default properties.
mutex(const string &name)
Initializes a new instance of the xtd::threading::mutex class with a string that is the name of the m...
auto unlock() -> void
Unlocks the mutex.
auto wait(int32 milliseconds_timeout) -> bool override
wait ownership of the specified mutex object.
auto close() -> void override
Releases all resources held by the current xtd::threading::wait_handle.
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 whethe...
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
auto signal() -> bool override
Releases ownership of the specified wait_handle object.
auto release_mutex() -> void
Releases the Mutex once.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:49
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
wait_handle()=default
Initializes a new instance of the xtd::threading::wait_handle class.
Represents a time interval.
Definition time_span.hpp:29
Contains xtd::date_time class.
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
std::intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::threading::wait_handle class.