TEST_F(TaskProcessorTest, Interrupt) {
std::unique_ptr<ThreadPool> thread_pool(ThreadPool::Create("task processor test", 1U));
Thread* const self = Thread::Current();
TaskProcessor task_processor; static constexpr size_t kRecursion = 10;
Atomic<bool> done_running(false);
Atomic<size_t> counter(0);
task_processor.AddTask(self, new RecursiveTask(&task_processor, &counter, kRecursion));
task_processor.Start(self); // Add a task which will wait until interrupted to the thread pool.
thread_pool->AddTask(self, new WorkUntilDoneTask(&task_processor, &done_running));
thread_pool->StartWorkers(self);
ASSERT_FALSE(done_running); // Wait until all the tasks are done, but since we didn't interrupt, done_running should be 0. while (counter.load(std::memory_order_seq_cst) != kRecursion) {
usleep(10);
}
ASSERT_FALSE(done_running);
task_processor.Stop(self);
thread_pool->Wait(self, true, false); // After the interrupt and wait, the WorkUntilInterruptedTasktask should have terminated and // set done_running_ to true.
ASSERT_TRUE(done_running.load(std::memory_order_seq_cst));
// Test that we finish remaining tasks before returning from RunTasksUntilInterrupted.
counter.store(0, std::memory_order_seq_cst);
done_running.store(false, std::memory_order_seq_cst); // Self interrupt before any of the other tasks run, but since we added them we should keep on // working until all the tasks are completed.
task_processor.Stop(self);
task_processor.AddTask(self, new RecursiveTask(&task_processor, &counter, kRecursion));
thread_pool->AddTask(self, new WorkUntilDoneTask(&task_processor, &done_running));
thread_pool->StartWorkers(self);
thread_pool->Wait(self, true, false);
ASSERT_TRUE(done_running.load(std::memory_order_seq_cst));
ASSERT_EQ(counter.load(std::memory_order_seq_cst), kRecursion);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.