#ifndef VISUALDL_BACKEND_UTILS_CONCURRENCY_H #define VISUALDL_BACKEND_UTILS_CONCURRENCY_H #include #include #include #include #include namespace visualdl { namespace cc { /* * Run a task every `duration` milliseconds. * Each evoke will start a thread to do this asynchronously. */ struct PeriodExector { using task_t = std::function; static PeriodExector& Global() { static PeriodExector exec; return exec; } void Quit() { // TODO use some conditonal variable to help quit immediately. quit = true; } void Start() { quit = false; } void operator()(task_t&& task, int msec) { auto task_wrapper = [=] { while (!quit) { if (!task()) break; std::this_thread::sleep_for(std::chrono::milliseconds(msec)); } LOG(INFO) << "quit job"; }; threads_.emplace_back(std::thread(std::move(task_wrapper))); msec_ = msec; } ~PeriodExector() { for (auto& t : threads_) { if (t.joinable()) { t.join(); } } } private: bool quit = false; std::vector threads_; int msec_; }; } // namespace cc } // namespace visualdl #endif