xtd 0.2.0
Loading...
Searching...
No Matches
interlocked.cpp

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

#include <xtd/threading/interlocked>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/random>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace interlocked_example {
struct my_thread {
string name;
};
class my_interlocked_exchange_class {
private:
//0 for False, 1 for True.
inline static int using_resource = 0;
static constexpr int num_thread_iterations = 5;
public:
// The main entry point for the application.
static auto main() {
auto my_threads = std::array<my_thread, 10> {};
auto rnd = xtd::random {};
for (auto index = 0ul; index < my_threads.size(); ++index) {
my_threads[index].name = string::format("Thread{}", index + 1);
//Wait a random amount of time before starting next thread.
thread::sleep(rnd.next(0, 1000));
my_threads[index].thread = thread {my_thread_proc};
my_threads[index].thread.start(my_threads[index].name);
}
for (auto index = 0ul; index < my_threads.size(); ++index)
my_threads[index].thread.join();
}
static void my_thread_proc(std::any name) {
for (auto index = 0; index < num_thread_iterations; ++index) {
use_resource(as<string>(name));
//Wait 1 second before next attempt.
thread::sleep(1000);
}
}
//A simple method that denies reentrancy.
static auto use_resource(const string& name)->bool {
//0 indicates that the method is not in use.
if (0 == interlocked::exchange(using_resource, 1)) {
console::write_line("{} acquired the lock", name);
//Code to access a resource that is not thread safe would go here.
//Simulate some work
thread::sleep(500);
console::write_line("{} exiting lock", name);
//Release the lock
interlocked::exchange(using_resource, 0);
return true;
} else {
console::write_line(" {} was denied the lock", name);
return false;
}
}
};
}
startup_(interlocked_example::my_interlocked_exchange_class::main);
// This example produces output similar to the following:
//
// Thread1 acquired the lock
// Thread1 exiting lock
// Thread2 acquired the lock
// Thread3 was denied the lock
// Thread2 exiting lock
// Thread1 acquired the lock
// Thread4 was denied the lock
// Thread5 was denied the lock
// Thread1 exiting lock
// Thread6 acquired the lock
// Thread3 was denied the lock
// Thread2 was denied the lock
// Thread7 was denied the lock
// Thread6 exiting lock
// Thread4 acquired the lock
// Thread5 was denied the lock
// Thread1 was denied the lock
// Thread4 exiting lock
// Thread8 acquired the lock
// Thread3 was denied the lock
// Thread2 was denied the lock
// Thread7 was denied the lock
// Thread6 was denied the lock
// Thread9 was denied the lock
// Thread8 exiting lock
// Thread5 acquired the lock
// Thread1 was denied the lock
// Thread4 was denied the lock
// Thread3 was denied the lock
// Thread10 was denied the lock
// Thread2 was denied the lock
// Thread7 was denied the lock
// Thread5 exiting lock
// Thread6 acquired the lock
// Thread9 was denied the lock
// Thread8 was denied the lock
// Thread1 was denied the lock
// Thread6 exiting lock
// Thread4 acquired the lock
// Thread3 was denied the lock
// Thread10 was denied the lock
// Thread2 was denied the lock
// Thread7 was denied the lock
// Thread5 was denied the lock
// Thread4 exiting lock
// Thread9 acquired the lock
// Thread8 was denied the lock
// Thread6 was denied the lock
// Thread9 exiting lock
// Thread10 acquired the lock
// Thread7 was denied the lock
// Thread5 was denied the lock
// Thread4 was denied the lock
// Thread8 was denied the lock
// Thread10 exiting lock
// Thread6 acquired the lock
// Thread9 was denied the lock
// Thread6 exiting lock
// Thread8 acquired the lock
// Thread10 was denied the lock
// Thread9 was denied the lock
// Thread8 exiting lock
// Thread10 acquired the lock
// Thread10 exiting lock
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:40
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:43
thread(const xtd::threading::parameterized_thread_start &start)
Initializes a new instance of the xtd::threading::thread class, specifying a delegate that allows an ...
void join()
Blocks the calling thread until this thread object terminates, while continuing to perform standard C...
#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:175
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10