Try to replicate subtle lifetime issues when destroying a TaskGroup where all tasks may not have finished running.
| 232 | // Try to replicate subtle lifetime issues when destroying a TaskGroup |
| 233 | // where all tasks may not have finished running. |
| 234 | void StressTaskGroupLifetime(std::function<std::shared_ptr<TaskGroup>()> factory) { |
| 235 | const int NTASKS = 100; |
| 236 | auto task_group = factory(); |
| 237 | auto weak_group_ptr = std::weak_ptr<TaskGroup>(task_group); |
| 238 | |
| 239 | std::atomic<bool> barrier(false); |
| 240 | |
| 241 | BarrierTask task{&barrier, weak_group_ptr, Status::OK()}; |
| 242 | |
| 243 | for (int i = 0; i < NTASKS; ++i) { |
| 244 | task_group->Append(task); |
| 245 | } |
| 246 | |
| 247 | // Lose strong reference |
| 248 | barrier.store(true); |
| 249 | task_group.reset(); |
| 250 | |
| 251 | // Wait for finish |
| 252 | while (!weak_group_ptr.expired()) { |
| 253 | SleepFor(1e-5); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Same, but with also a failing task |
| 258 | void StressFailingTaskGroupLifetime(std::function<std::shared_ptr<TaskGroup>()> factory) { |