xtd 1.0.0
Loading...
Searching...
No Matches
thread_local_object.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "mutex.hpp"
6#include "thread.hpp"
7#include "../func.hpp"
8#include "../object.hpp"
9#include "../time_span.hpp"
10#include <map>
11
13namespace xtd {
15 namespace threading {
38 template<typename value_t>
39 class thread_local_object : public object {
40 struct data : public object {
42 bool track_all_values = false;
43 func<value_t> value_factory;
44 std::map<intptr, value_t> values;
45 };
46
47 struct lock_guard_mutex {
48 lock_guard_mutex(mutex& mutex) : mutex_(mutex) {mutex_.lock();}
49 ~lock_guard_mutex() {mutex_.unlock();}
50 private:
51 mutex& mutex_;
52 };
53
54 public:
56
65 explicit thread_local_object(bool track_all_values) {data_->track_all_values = track_all_values;}
68 explicit thread_local_object(const func<value_t>& value_factory) {data_->value_factory = value_factory;}
73 thread_local_object(const func<value_t>& value_factory, bool track_all_values) {
74 data_->value_factory = value_factory;
75 data_->track_all_values = track_all_values;
76 }
77
78
80 template<typename func_t>
81 explicit thread_local_object(func_t value_factory) : thread_local_object(func<value_t> {value_factory}) {}
82 template<typename func_t>
83 explicit thread_local_object(func_t value_factory, bool track_all_values) : thread_local_object(func<value_t> {value_factory}, track_all_values) {}
86 auto operator =(const thread_local_object& other) -> thread_local_object& = default;
88
90
94 [[nodiscard]] auto is_value_created() const noexcept -> bool {
95 lock_guard_mutex lock {data_->mutex};
96 return data_->values.find(thread::current_thread().thread_id()) != data_->values.end();
97 }
98
102 [[nodiscard]] auto value() const noexcept -> value_t {
103 lock_guard_mutex lock {data_->mutex};
104 if (!is_value_created()) data_->values[thread::current_thread().thread_id()] = data_->value_factory.is_empty() ? value_t {} : data_->values[thread::current_thread().thread_id()] = data_->value_factory();
105 return data_->values.find(thread::current_thread().thread_id())->second;
106 }
107
110 auto value(value_t value) noexcept -> void {
111 lock_guard_mutex lock {data_->mutex};
112 data_->values[thread::current_thread().thread_id()] = value;
113 }
114
119 [[nodiscard]] auto values() const -> xtd::array<value_t> {
121 lock_guard_mutex lock {data_->mutex};
122 auto values = xtd::array<value_t> {};
123 for (const auto& entry : data_->values)
124 values.resize(values.length() + 1, entry.second);
125 return values;
126 }
127
128
130
132 [[nodiscard]] auto to_string() const noexcept -> string override {
133 return string::format("{}", value());
134 }
135
136
137 private:
138 mutable xtd::sptr<data> data_ = xtd::new_sptr<data>();
139 };
140 }
141}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
object()=default
Create a new instance of the ultimate base class object.
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock.hpp:32
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.hpp:50
auto lock() -> void
Locks the mutex. If another thread has already locked the mutex, a call to lock will block execution ...
auto unlock() -> void
Unlocks the mutex.
Provides thread-local storage of data.
Definition thread_local_object.hpp:39
thread_local_object(bool track_all_values)
Initializes the xtd::threading::thread_local_object instance and specifies whether all values are acc...
Definition thread_local_object.hpp:65
auto value(value_t value) noexcept -> void
Sets the value of this instance for the current thread.
Definition thread_local_object.hpp:110
thread_local_object(const func< value_t > &value_factory, bool track_all_values)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.hpp:73
auto value() const noexcept -> value_t
Gets the value of this instance for the current thread.
Definition thread_local_object.hpp:102
thread_local_object()=default
Initializes the xtd::threading::thread_local_object instance.
auto to_string() const noexcept -> string override
Returns a xtd::string that represents the current object.
Definition thread_local_object.hpp:132
auto values() const -> xtd::array< value_t >
Gets a list containing the values stored by all threads that have accessed this instance.
Definition thread_local_object.hpp:119
auto is_value_created() const noexcept -> bool
Gets whether xtd::threading::thread_local_object::value is initialized on the current thread.
Definition thread_local_object.hpp:94
thread_local_object(const func< value_t > &value_factory)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.hpp:68
static auto current_thread() noexcept -> thread &
Gets the currently running thread.
Contains xtd::func delegate.
xtd::delegate< result_t(arguments_t... arguments)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.hpp:27
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
auto new_sptr(args_t &&... args) -> xtd::sptr< type_t >
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
Contains xtd::threading::mutex exception.
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::object class.
Contains xtd::threading::thread class.
Contains xtd::time_span class.