xtd 0.2.0
Loading...
Searching...
No Matches
xtd::threading::lock_guard Class Referencefinal
Inheritance diagram for xtd::threading::lock_guard:
xtd::object

Definition

Provides a mechanism that synchronizes access to objects with xtd::threading::mutex.

class core_export_ lock_guard final : public xtd::object
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
lock_guard(const object_t &obj)
Create a xtd::threading::lock_guard object and acquires an exclusive lock_guard on the specified obj.
Definition lock_guard.hpp:44
#define core_export_
Define shared library export.
Definition core_export.hpp:13
Inheritance
xtd::object → xtd::lock_guard
Header
#include <xtd/threading/lock_guard>
Namespace
xtd::threading
Library
xtd.core
Examples
The following example uses the xtd::threading::lock_guard class to synchronize access to a single instance of a random number generator represented by the xtd::random class. The example creates ten threads, each of which executes asynchronously on a thread pool thread. Each thread generates 10,000 random numbers, calculates their average, and updates two procedure-level variables that maintain a running total of the number of random numbers generated and their sum. After all threads have executed, these two values are then used to calculate the overall mean.
#include <xtd/xtd>
namespace lock_guard_example {
class program {
public:
static void main() {
auto threads = list<thread> {};
auto rnd = xtd::random {};
auto total = 0_s64;
auto n = 0;
for (auto thread_ctr = 0; thread_ctr < 10; ++thread_ctr)
threads.push_back(thread::start_new([&] {
auto values = std::array<int, 10000> {};
auto thread_total = 0;
auto thread_n = 0;
auto ctr = 0;
block_scope_(lock_guard lock {rnd}) {
// Generate 10,000 random integers
for (ctr = 0; ctr < 10000; ++ctr)
values[ctr] = rnd.next(0, 1001);
}
thread_n = ctr;
for (auto value : values)
thread_total += value;
console::write_line("Mean for task {0,2}: {1:N2} (N={2:N0})",
thread::current_thread().managed_thread_id(),
(thread_total * 1.0) / thread_n, thread_n);
interlocked::add(n, thread_n);
interlocked::add(total, thread_total);
}));
try {
for (auto& thread : threads)
thread.join();
console::write_line("\nMean for all tasks: {0:N2} (N={1:N0})",
(total * 1.0)/n, n);
}
catch (const system_exception& e) {
console::write_line("{0}: {1}", e.get_type().name(), e.message());
}
}
};
}
startup_(lock_guard_example::program::main);
// This example produces output similar to the following:
//
// Mean for task 4: 498.90 (N=10000)
// Mean for task 2: 499.92 (N=10000)
// Mean for task 7: 503.12 (N=10000)
// Mean for task 5: 499.41 (N=10000)
// Mean for task 3: 498.58 (N=10000)
// Mean for task 8: 496.71 (N=10000)
// Mean for task 10: 501.49 (N=10000)
// Mean for task 6: 498.84 (N=10000)
// Mean for task 9: 502.72 (N=10000)
// Mean for task 11: 498.69 (N=10000)
//
// Mean for all tasks: 499.84 (N=100000)
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:44
#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:168
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.hpp:25
xtd::int32 managed_thread_id() noexcept
Gets the managed thread id of the current thread.
@ n
The N key.
Definition console_key.hpp:114
@ e
The E key.
Definition console_key.hpp:96
Remarks
See xtd::threading::mutex for more information.

Public Constructors

template<class object_t>
 lock_guard (const object_t &obj)
 Create a xtd::threading::lock_guard object and acquires an exclusive lock_guard on the specified obj.
 
 lock_guard (const xtd::string &str)
 Create a xtd::threading::lock_guard object and acquires an exclusive lock_guard on the specified string.
 

Public Methods

void enter ()
 Enters the lock_guard, waiting if necessary until the lock_guard can be entered.
 
void exit ()
 Exits the lock_guard.
 

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual xtd::size 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<class object_t>
xtd::unique_ptr_object< 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.
 
template<class object_a_t, class 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<class object_a_t, class 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.
 

Constructor & Destructor Documentation

◆ lock_guard() [1/2]

template<class object_t>
xtd::threading::lock_guard::lock_guard ( const object_t & obj)
inlineexplicit

Create a xtd::threading::lock_guard object and acquires an exclusive lock_guard on the specified obj.

Parameters
objThe object on which to acquire the mutex lock_guard.
Remarks
When the xtd::threading::lock_guard destroyed it releases the exclusive lock_guard specified in the constructor.

◆ lock_guard() [2/2]

xtd::threading::lock_guard::lock_guard ( const xtd::string & str)
explicit

Create a xtd::threading::lock_guard object and acquires an exclusive lock_guard on the specified string.

Parameters
strThe string on which to acquire the mutex lock_guard.
Remarks
When the xtd::threading::lock_guard destroyed it releases the exclusive lock_guard specified in the constructor.

Member Function Documentation

◆ enter()

void xtd::threading::lock_guard::enter ( )

Enters the lock_guard, waiting if necessary until the lock_guard can be entered.

Remarks
When the method returns, the current thread is the only thread that holds the lock_guard. If the lock_guard can't be entered immediately, the method waits until the lock_guard can be entered. If the lock_guard is already held by the current thread, the lock_guard is entered again. To fully exit the lock_guard and allow other threads to enter the lock_guard, the current thread should exit the lock_guard as many times as it has entered the lock_guard.

◆ exit()

void xtd::threading::lock_guard::exit ( )

Exits the lock_guard.

Exceptions
xtd::object_closed_exceptionthe handle is invalid
Remarks
If the current thread holds the lock_guard multiple times, such as recursively, the lock_guard is exited only once. The current thread should ensure that each enter is matched with an exit.

The documentation for this class was generated from the following file: