@@ -52,9 +52,13 @@ class ThreadPool {
52
52
*/
53
53
class ThreadPoolTask {
54
54
public:
55
- ThreadPoolTask () = default ;
55
+ ThreadPoolTask () = delete ;
56
56
ThreadPoolTask (ThreadPool* tp)
57
- : tp_(tp){};
57
+ : tp_(tp) {
58
+ if (tp == nullptr ) {
59
+ throw std::runtime_error (" Cannot construct task for a null threadpool" );
60
+ }
61
+ };
58
62
virtual ~ThreadPoolTask (){};
59
63
60
64
protected:
@@ -79,11 +83,15 @@ class ThreadPool {
79
83
*/
80
84
class Task : public ThreadPoolTask {
81
85
public:
86
+ Task () = delete ;
87
+
82
88
/* *
83
- * Default constructor
84
- * @brief Default constructed SharedTask is possible but not valid().
89
+ * Constructor
90
+ * @brief Initializes only the threadpool but the future it encapsulates is
91
+ * not valid().
85
92
*/
86
- Task () = default ;
93
+ Task (ThreadPool* tp)
94
+ : ThreadPoolTask(tp){};
87
95
88
96
/* *
89
97
* Constructor from std::future
@@ -165,11 +173,15 @@ class ThreadPool {
165
173
*/
166
174
class SharedTask : public ThreadPoolTask {
167
175
public:
176
+ SharedTask () = delete ;
177
+
168
178
/* *
169
- * Default constructor
170
- * @brief Default constructed SharedTask is possible but not valid().
179
+ * Constructor
180
+ * @brief Initializes only the threadpool but the shared_future it
181
+ * encapsulates is not valid().
171
182
*/
172
- SharedTask () = default ;
183
+ SharedTask (ThreadPool* tp)
184
+ : ThreadPoolTask(tp){};
173
185
174
186
/* *
175
187
* Constructor from std::shared_future
@@ -318,7 +330,7 @@ class ThreadPool {
318
330
template <class Fn , class ... Args>
319
331
auto async (Fn&& f, Args&&... args) {
320
332
if (concurrency_level_ == 0 ) {
321
- Task invalid_future;
333
+ Task invalid_future ( this ) ;
322
334
LOG_ERROR (" Cannot execute task; thread pool uninitialized." );
323
335
return invalid_future;
324
336
}
0 commit comments