36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
#pragma once
|
|
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <thread>
|
|
#include "Core/Statistics/Frequency_Limit.h"
|
|
|
|
|
|
struct Detach_Thread {
|
|
explicit Detach_Thread(const std::string& name, const std::function<void(std::atomic<bool>& running)>& func);
|
|
void start();
|
|
std::string name;
|
|
std::thread thread;
|
|
std::atomic<bool> running = true; //控制运行
|
|
std::atomic<bool> exit = false;
|
|
};
|
|
struct Detach_Thread_Manager {
|
|
Detach_Thread_Manager() = default;
|
|
void test_and_start_thread(const std::string& name, const std::function<void(std::atomic<bool>& running)>& func);
|
|
void test_and_stop_thread(const std::string& name);
|
|
void stop_all_thread(size_t timeout_milliseconds = 10000, std::set<std::string> excluded_thread = {});
|
|
Detach_Thread* get_thread(const std::thread::id thread_id);
|
|
protected:
|
|
Frequency_Limit fl;
|
|
std::map<std::string, Detach_Thread*> thread_map;
|
|
void exit_thread(const std::string& name);
|
|
void start_thread(const std::string &name, const std::function<void(std::atomic<bool> &)> &f);
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> exit_time;
|
|
};
|