#include <xtd/threading/interlocked>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
namespace interlocked_decrement_example {
class my_interlocked_decrement_class {
private:
class count_class {
public:
inline static int unsafe_instance_count = 0;
inline static int safe_instance_count = 0;
count_class() {
unsafe_instance_count++;
}
~count_class() {
unsafe_instance_count--;
}
};
public:
static auto main() {
auto thread1 =
thread {thread_method};
auto thread2 =
thread {thread_method};
thread2.start();
thread1.join();
thread2.join();
console::write_line(
"unsafe_instance_count: {}\nsafe_instance_count: {}", count_class::unsafe_instance_count, count_class::safe_instance_count);
}
private:
static void thread_method() {
auto cc = count_class {};
for (
auto i = 0;
i < 100000; ++
i)
cc = count_class {};
}
};
}
startup_(interlocked_decrement_example::my_interlocked_decrement_class::main);
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static int32 increment(int32 &location) noexcept
Increments a specified variable and stores the result, as an atomic operation.
static int32 decrement(int32 &location) noexcept
Decrements a specified variable and stores the result, as an atomic operation.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:45
void start()
Causes the operating system to change the state of the current instance to xtd::threading::thread_sta...
#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
@ i
The I key.
Definition console_key.hpp:104
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