xtd 0.2.0
mutex.cpp

Shows how to use xtd::threading::mutex class.

#include <xtd/collections/generic/list>
#include <xtd/threading/mutex>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::threading;
namespace mutex_example {
class program {
public:
static void main() {
// Create the threads that will use the protected resource.
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:
// This method represents a resource that must be synchronized
// so that only one thread at a time can enter.
static void Use_resource() {
// Wait until it is safe to enter.
console::write_line("{0} is requesting the mutex",
mut.wait_one();
console::write_line("{0} has entered the protected area",
// Place code to access non-reentrant resources here.
// Simulate some work.
console::write_line("{0} is leaving the protected area",
// Release the mutex.
mut.release_mutex();
console::write_line("{0} has released the mutex",
}
inline static list<thread> threads;
// Create a new mutex. The creating thread does not own the mutex.
inline static mutex mut;
inline static constexpr int num_iterations = 1;
inline static constexpr int num_threads = 3;
};
}
startup_(mutex_example::program::main);
// This example produces output similar to the following:
//
// thread_1 is requesting the mutex
// thread_1 has entered the protected area
// thread_2 is requesting the mutex
// thread_3 is requesting the mutex
// thread_1 is leaving the protected area
// thread_1 has released the mutex
// thread_2 has entered the protected area
// thread_2 is leaving the protected area
// thread_2 has released the mutex
// thread_3 has entered the protected area
// thread_3 is leaving the protected area
// thread_3 has released the mutex
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:79
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.hpp:50
static thread & current_thread() noexcept
Gets the currently running thread.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
static void join_all()
Blocks the calling thread until all joinable threads terminate.
#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:167
@ i
The I key.
Definition console_key.hpp:104
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
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