xtd 0.2.0
Loading...
Searching...
No Matches
xtd::threading::thread_local_object< value_t > Class Template Reference
Inheritance diagram for xtd::threading::thread_local_object< value_t >:
xtd::object

Definition

template<typename value_t>
class xtd::threading::thread_local_object< value_t >

Provides thread-local storage of data.

template<typename value_t>
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42
Provides thread-local storage of data.
Definition thread_local_object.hpp:40
Header
#include <xtd/threading/thread_local_object>
Namespace
xtd::threading
Library
xtd.core
Examples
The following example shows how to use a xtd::threading::thread_local_object:
Remarks
All public and protected members of xtd::threading::thread_local_object are thread-safe and may be used concurrently from multiple threads. The value returned for the xtd::threading::thread_local_object::value and xtd::threading::thread_local_object::is_value_created properties is specific for the thread on which the property is accessed.
The differences between the thread_local_object class and the C++ thread_local keyword are :
  • It is possible to declare a member variable of a thread local class with the thread_local_object class, whereas this is not possible with the thread_local keyword.
  • With the thread_local_object class, you can consult the various values stored by all threads.
Warning
Each thread that has ever accessed the instance will contribute to the values stored in the instance. This includes threads that have since left the instance

Public Constructors

 thread_local_object ()=default
 Initializes the xtd::threading::thread_local_object instance.
 
 thread_local_object (bool track_all_values)
 Initializes the xtd::threading::thread_local_object instance and specifies whether all values are accessible from any thread.
 
 thread_local_object (const func< value_t > &value_factory)
 Initializes the xtd::threading::thread_local_object instance with the specified value_factory function.
 
 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 function and a flag that indicates whether all values are accessible from any thread.
 

Public Properties

bool is_value_created () const noexcept
 Gets whether xtd::threading::thread_local_object::value is initialized on the current thread.
 
value_t value () const noexcept
 Gets the value of this instance for the current thread.
 
void value (value_t value) noexcept
 Sets the value of this instance for the current thread.
 
std::vector< value_t > values () const
 Gets a list containing the values stored by all threads that have accessed this instance.
 

Public Methods

string to_string () const noexcept override
 Returns a xtd::string that represents the current object.
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 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 size_t 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<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename 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<typename object_a_t , typename 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

◆ thread_local_object() [1/4]

template<typename value_t >
xtd::threading::thread_local_object< value_t >::thread_local_object ( )
default

Initializes the xtd::threading::thread_local_object instance.

Remarks
The default value of value_t is used to initialize the instance when xtd::threading::thread_local_object::value is accessed for the first time.
This constructor is equivalent to calling the xtd::threading::thread_local_object(bool) constructor with a value of false for the track_all_values argument.

◆ thread_local_object() [2/4]

template<typename value_t >
xtd::threading::thread_local_object< value_t >::thread_local_object ( bool  track_all_values)
inlineexplicit

Initializes the xtd::threading::thread_local_object instance and specifies whether all values are accessible from any thread.

Parameters
track_all_valuestrue to track all values set on the instance and expose them through the xtd::threading::thread_local_object::values property; false otherwise. When set to true, a value stored from a given thread will be available through xtd::threading::thread_local_object::values even after that thread has exited.
Remarks
If track_all_values is false, only the value of this instance for the current thread is accessible. Attempting to use the xtd::threading::thread_local_object::values property to retrieve all values throws an xtd::invalid_operation_exception exception.

◆ thread_local_object() [3/4]

template<typename value_t >
xtd::threading::thread_local_object< value_t >::thread_local_object ( const func< value_t > &  value_factory)
inlineexplicit

Initializes the xtd::threading::thread_local_object instance with the specified value_factory function.

Parameters
value_factoryThe xtd::func invoked to produce a lazily-initialized value when an attempt is made to retrieve xtd::threading::thread_local_object::value without it having been previously initialized.

◆ thread_local_object() [4/4]

template<typename value_t >
xtd::threading::thread_local_object< value_t >::thread_local_object ( const func< value_t > &  value_factory,
bool  track_all_values 
)
inline

Initializes the xtd::threading::thread_local_object instance with the specified value_factory function and a flag that indicates whether all values are accessible from any thread.

Parameters
value_factoryThe xtd::func invoked to produce a lazily-initialized value when an attempt is made to retrieve Value without it having been previously initialized.
track_all_valuestrue to track all values set on the instance and expose them through the xtd::threading::thread_local_object::values property; false otherwise. When set to true, a value stored from a given thread will be available through xtd::threading::thread_local_object::values even after that thread has exited.
Remarks
If track_all_values is false, only the value of this instance for the current thread is accessible. Attempting to use the xtd::threading::thread_local_object::values property to retrieve all values throws an xtd::invalid_operation_exception exception.

Member Function Documentation

◆ is_value_created()

template<typename value_t >
bool xtd::threading::thread_local_object< value_t >::is_value_created ( ) const
inlinenoexcept

Gets whether xtd::threading::thread_local_object::value is initialized on the current thread.

Returns
true if xtd::threading::thread_local_object::value is initialized on the current thread; otherwise false.

◆ value() [1/2]

template<typename value_t >
value_t xtd::threading::thread_local_object< value_t >::value ( ) const
inlinenoexcept

Gets the value of this instance for the current thread.

Returns
Returns an instance of the object that this xtd::threading::thread_local_object::value is responsible for initializing.
Remarks
If this instance was not previously initialized for the current thread, accessing xtd::threading::thread_local_object::value will attempt to initialize it. If an initialization function was supplied during the construction, that initialization will happen by invoking the function to retrieve the initial value for xtd::threading::thread_local_object::value. Otherwise, the default value of value_t will be used.

◆ value() [2/2]

template<typename value_t >
void xtd::threading::thread_local_object< value_t >::value ( value_t  value)
inlinenoexcept

Sets the value of this instance for the current thread.

Parameters
valueAn instance of the object that this xtd::threading::thread_local_object::value is responsible for initializing.
Remarks
If this instance was not previously initialized for the current thread, accessing xtd::threading::thread_local_object::value will attempt to initialize it. If an initialization function was supplied during the construction, that initialization will happen by invoking the function to retrieve the initial value for xtd::threading::thread_local_object::value. Otherwise, the default value of value_t will be used.

◆ values()

template<typename value_t >
std::vector< value_t > xtd::threading::thread_local_object< value_t >::values ( ) const
inline

Gets a list containing the values stored by all threads that have accessed this instance.

Returns
A list for all of the values stored by all of the threads that have accessed this instance.
Exceptions
xtd::invalid_operation_exceptionValues stored by all threads are not available because this instance was initialized with the track_all_values argument set to false in the call to a class constructor.
Remarks
Each thread that has ever accessed this instance will contribute to this list the value last stored into the instance. This includes threads that have since exited.

◆ to_string()

template<typename value_t >
string xtd::threading::thread_local_object< value_t >::to_string ( ) const
inlineoverridevirtualnoexcept

Returns a xtd::string that represents the current object.

Returns
A string that represents the current object.
Examples
The following code example demonstrates what to_string returns.
#include <xtd/xtd>
using namespace xtd;
namespace examples {
namespace object_test {
class object1 : public object {
};
}
}
auto main() -> int {
ptr<object> obj1 = new_ptr<examples::object_test::object1>();
console::write_line(obj1->to_string());
ptr<object> obj2 = new_ptr<date_time>(1971, 1, 5, 23, 5, 0);
console::write_line(obj2->to_string());
ptr<object> obj3 = new_ptr<boolean_object>();
console::write_line(obj3->to_string());
}
// This code produces the following output :
//
// examples::object_test::object1
// Tue Jan 5 23:05:00 1971
// false
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10

Reimplemented from xtd::object.


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