93 lines
2.5 KiB
C++
Executable File
93 lines
2.5 KiB
C++
Executable File
#ifndef LIBAPI_PROCESS_THREAD_H
|
|
#define LIBAPI_PROCESS_THREAD_H
|
|
|
|
#include <thread>
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <condition_variable>
|
|
#include "LibapiThread.h"
|
|
#include "LibapiQueue.h"
|
|
#include "LibapiQueues.h"
|
|
|
|
struct Signal
|
|
{
|
|
int id;
|
|
bool end = false;
|
|
};
|
|
|
|
class LibapiProcessThread;
|
|
typedef LibapiProcessThread ProThread;
|
|
typedef std::shared_ptr<LibapiProcessThread> SharedThread;
|
|
|
|
// 全局线程管理
|
|
static std::vector<SharedThread> shared_threads;
|
|
static std::mutex g_mutex;
|
|
|
|
class LibapiProcessThread : public LibapiThread
|
|
{
|
|
public:
|
|
static std::vector<SharedThread>* get_shared_threads()
|
|
{
|
|
return &shared_threads;
|
|
};
|
|
|
|
template<class T>
|
|
static std::shared_ptr<LibapiProcessThread> create(std::string thread_name = "")
|
|
{
|
|
std::unique_lock<std::mutex> locker(g_mutex);
|
|
|
|
T* plugin = new T();
|
|
|
|
std::string type_name;
|
|
if (thread_name == "") type_name = typeid(plugin).name();
|
|
else type_name = thread_name;
|
|
|
|
SharedThread ptr = std::make_shared<LibapiProcessThread>(-1, type_name,(IRunnable*)plugin);
|
|
shared_threads.push_back(ptr);
|
|
return ptr;
|
|
};
|
|
|
|
template<class T>
|
|
static std::shared_ptr<LibapiProcessThread> create_and_start(std::string thread_name = "")
|
|
{
|
|
std::unique_lock<std::mutex> locker(g_mutex);
|
|
|
|
T* plugin = new T();
|
|
|
|
std::string type_name;
|
|
if (thread_name == "") type_name = typeid(plugin).name();
|
|
else type_name = thread_name;
|
|
|
|
SharedThread ptr = std::make_shared<LibapiProcessThread>(-1, type_name, (BaseRunnable*)plugin);
|
|
ptr->start();
|
|
shared_threads.push_back(ptr);
|
|
return ptr;
|
|
};
|
|
|
|
public:
|
|
LibapiProcessThread(int id, std::string name, BaseRunnable* runnable);
|
|
virtual ~LibapiProcessThread();
|
|
void set_parent_thread(LibapiProcessThread* pThread);
|
|
void add_sub_thread(LibapiProcessThread* pParentThread);
|
|
void set_next_thread(LibapiProcessThread* pSubThread);
|
|
void update_sub_thread_signal(LibapiProcessThread* pThread);
|
|
void run(); // override function
|
|
|
|
private:
|
|
void reset_sub_thread_signal(std::vector<Signal>& sub_thread_signal);
|
|
bool check_all_sub_complete();
|
|
|
|
private:
|
|
LibapiProcessThread* m_parent = NULL;
|
|
std::vector<LibapiProcessThread*> m_sub_threads;
|
|
std::vector<Signal> m_sub_thread_signal;
|
|
//LibapiProcessThread* m_next_thread = NULL;
|
|
std::vector<LibapiProcessThread*> m_next_threads;
|
|
int m_cur_index = 0;
|
|
std::mutex m_sig_mutex;
|
|
};
|
|
|
|
#endif // LIBAPITHREAD_H
|