Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ThreadPrivate.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ThreadPrivate.h (C) 2000-2025 */
9/* */
10/* Class for storing a specific value per thread. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_CONCURRENCY_THREADPRIVATE_H
13#define ARCCORE_CONCURRENCY_THREADPRIVATE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include "arccore/concurrency/GlibAdapter.h"
20
21#include <vector>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*!
33 * \brief Container for thread-private values.
34 *
35 * initialize() must be called before using the setValue()/getValue() methods.
36 * This initialize() method can be called multiple times.
37 *
38 * \deprecated Use 'thread_local' from C++11.
39 */
40class ARCCORE_CONCURRENCY_EXPORT ThreadPrivateStorage
41{
42 public:
43
44 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
45 ThreadPrivateStorage();
46 ~ThreadPrivateStorage();
47
48 public:
49
50 /*!
51 * \brief Initializes the key containing thread-private values.
52 * This method can be called multiple times and does nothing if
53 * the key has already been initialized.
54 *
55 * \warning This method is not thread-safe. The user must therefore
56 * be careful during the first call.
57 */
58 void initialize();
59 void* getValue();
60 void setValue(void* v);
61
62 private:
63
64 GlibPrivate* m_storage;
65};
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
70/*!
71 * \brief Base class allowing an instance of an object to be stored per thread.
72 *
73 * \deprecated Use 'thread_local' from C++11.
74 */
75class ARCCORE_CONCURRENCY_EXPORT ThreadPrivateBase
76{
77 public:
78
80 {
81 public:
82
83 virtual ~ICreateFunctor() {}
84 virtual void* createInstance() = 0;
85 };
86
87 public:
88
89 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
90 ThreadPrivateBase(ThreadPrivateStorage* key, ICreateFunctor* create_functor)
91 : m_key(key)
92 , m_create_functor(create_functor)
93 {
94 }
95
97 {
98 }
99
100 public:
101
102 /*!
103 * \brief Retrieves the instance specific to the current thread.
104 *
105 * If it does not yet exist, it is created via
106 * the functor passed as an argument to the constructor.
107 *
108 * \warning This method must not be called until
109 * the associated key (ThreadPrivateStorage) has been initialized
110 * by calling ThreadPrivateStorage::initialize().
111 */
112 void* item();
113
114 private:
115
116 ThreadPrivateStorage* m_key;
117 GlibMutex m_mutex;
118 ICreateFunctor* m_create_functor;
119};
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124/*!
125 * \brief Class allowing an instance of a type per thread.
126 *
127 * The container allowing values to be stored must be passed as an argument
128 * to the constructor. This container must have been initialized
129 * via ThreadPrivateStorage::initialize() before using this class.
130 *
131 * This class only has one method item()
132 * allowing retrieval of an instance of type \a T per
133 * thread. On the first call to item() for a given thread,
134 * an instance of \a T is constructed.
135 * The type \a T must have a default constructor
136 * and must have a \a build() method.
137 * \threadsafeclass
138 *
139 * \deprecated Use 'thread_local' from C++11.
140 */
141template <typename T>
142class ThreadPrivate
144{
145 public:
146
147 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
148 ThreadPrivate(ThreadPrivateStorage* key)
149 : m_storage(key, this)
150 {
151 }
152
153 ~ThreadPrivate()
154 {
155 for (T* item : m_allocated_items)
156 delete item;
157 }
158
159 public:
160
161 //! Instance specific to the current thread.
162 T* item()
163 {
164 return (T*)(m_storage.item());
165 }
166
167 private:
168
169 void* createInstance() override
170 {
171 T* new_ptr = new T();
172 new_ptr->build();
173 m_allocated_items.push_back(new_ptr);
174 return new_ptr;
175 }
176
177 private:
178
179 std::vector<T*> m_allocated_items;
180 ThreadPrivateBase m_storage;
181};
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186} // namespace Arcane
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190
191#endif
Definitions and globals of Arccore.
Base class allowing an instance of an object to be stored per thread.
Container for thread-private values.
void initialize()
Initializes the key containing thread-private values. This method can be called multiple times and do...
T * item()
Instance specific to the current thread.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --