MySQL 26.10.0
Source Code Documentation
ut::Per_thread< Value > Class Template Reference

Registry of per-thread values owned by one enclosing object. More...

#include <ut0per_thread.h>

Public Member Functions

 Per_thread (std::function< Value()> factory)
 Create an empty registry. More...
 
 Per_thread (const Per_thread &)=delete
 
Per_thread & operator= (const Per_thread &)=delete
 
Value * operator-> ()
 Return this thread's Value, creating and storing it on first access. More...
 

Private Attributes

std::function< Value()> m_factory
 
std::mutex m_mutex
 
std::unordered_map< std::thread::id, Value > m_map
 

Detailed Description

template<typename Value>
class ut::Per_thread< Value >

Registry of per-thread values owned by one enclosing object.

This solves the case in which code needs one Value per participating thread, but the values must follow the lifetime of a registry. Plain thread_local is not enough, because thread-local objects outlive the registry and are destroyed only when their threads exit.

Constructor & Destructor Documentation

◆ Per_thread() [1/2]

template<typename Value >
ut::Per_thread< Value >::Per_thread ( std::function< Value()>  factory)
inlineexplicit

Create an empty registry.

Parameters
[in]factorycallable used to create the current thread's Value on the first access from that thread; must be non-empty; must not attempt calling the Per_thread::operator->() as it could lead to endless recursion.

◆ Per_thread() [2/2]

template<typename Value >
ut::Per_thread< Value >::Per_thread ( const Per_thread< Value > &  )
delete

Member Function Documentation

◆ operator->()

template<typename Value >
Value * ut::Per_thread< Value >::operator-> ( )
inline

Return this thread's Value, creating and storing it on first access.

Repeated calls from the same thread return the same object until the registry is destroyed.

As thread ids might be reused, it is possible that a new thread "inherits" the Value which was used by an old thread, instead of starting with a fresh one.

Returns
pointer to the Value owned by this registry for the calling thread

◆ operator=()

template<typename Value >
Per_thread & ut::Per_thread< Value >::operator= ( const Per_thread< Value > &  )
delete

Member Data Documentation

◆ m_factory

template<typename Value >
std::function<Value()> ut::Per_thread< Value >::m_factory
private

◆ m_map

template<typename Value >
std::unordered_map<std::thread::id, Value> ut::Per_thread< Value >::m_map
private

◆ m_mutex

template<typename Value >
std::mutex ut::Per_thread< Value >::m_mutex
private

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