cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TAsyncLoader.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27#include <condition_variable>
28#include <functional>
29#include <future>
30#include <mutex>
31
32namespace cppcore {
33
34//-------------------------------------------------------------------------------------------------
53//-------------------------------------------------------------------------------------------------
54template<class T>
56public:
58 using LoadFunc = std::function<T()>;
59
61 TAsyncLoader() = default;
62
65
66 // This class cannot be copied or moved.
68
69
72 std::future<T> load(LoadFunc loadFunc);
73
76 size_t numPending() const;
77
80 bool hasPending() const;
81
83 void waitForAll();
84
85private:
86 void finish();
87
88private:
89 mutable std::mutex mMutex;
90 std::condition_variable mCondition;
91 size_t mNumPending{0};
92};
93
94template<class T>
98
99template<class T>
100inline std::future<T> TAsyncLoader<T>::load(LoadFunc loadFunc) {
101 assert(loadFunc != nullptr);
102
103 {
104 std::lock_guard<std::mutex> lock(mMutex);
105 ++mNumPending;
106 }
107
108 try {
109 std::future<T> f = std::async(std::launch::async, [this, loadFunc]() -> T {
110 // Make sure the pending counter is decremented, even if the load throws.
111 struct Finalizer {
112 Finalizer(const Finalizer&) = delete;
113 Finalizer(const Finalizer&&) = delete;
114 Finalizer &operator = (const Finalizer&) = delete;
115
116 Finalizer() = default;
117 TAsyncLoader *mLoader{nullptr};
118 ~Finalizer() { mLoader->finish(); }
119 } finalizer{this};
120
121 return loadFunc();
122 });
123 return f;
124 } catch (std::runtime_error &) {
125 std::lock_guard<std::mutex> lock(mMutex);
126 --mNumPending;
127 }
128
129 return std::future<T>();
130}
131
132template<class T>
133inline size_t TAsyncLoader<T>::numPending() const {
134 std::lock_guard<std::mutex> lock(mMutex);
135 return mNumPending;
136}
137
138template<class T>
139inline bool TAsyncLoader<T>::hasPending() const {
140 return numPending() != 0;
141}
142
143template<class T>
145 std::unique_lock<std::mutex> lock(mMutex);
146 mCondition.wait(lock, [this]() { return mNumPending == 0; });
147}
148
149template<class T>
150inline void TAsyncLoader<T>::finish() {
151 // Notify while holding the lock so waitForAll() cannot return and destroy this
152 // instance while the notification is still in flight.
153 std::lock_guard<std::mutex> lock(mMutex);
154 --mNumPending;
155 mCondition.notify_all();
156}
157
158} // namespace cppcore
#define CPPCORE_NONE_COPYING(name)
This macro marks a class a not copyable.
TAsyncLoader()=default
The default class constructor.
std::function< T()> LoadFunc
The load function type, describes the blocking load operation to run asynchronously.
bool hasPending() const
Returns true, if there is at least one load request which has not finished yet.
~TAsyncLoader()
The class destructor, will wait for all pending loads to finish.
void waitForAll()
Blocks the calling thread until all pending load requests have finished.
size_t numPending() const
Returns the number of load requests which have not finished yet.
std::future< T > load(LoadFunc loadFunc)
Will start a new load request which runs asynchronously.