1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| #ifndef PREPOCESS_THREAD_H #define PREPOCESS_THREAD_H #include <iostream> #include <mutex> #include <thread>
#include "safeQueue.h" #include "algorithm.h"
using namespace std;
template <typename T, typename P> class AbstrctThreadAlg{ public: vector<AbstractAlgorithm<T, P>*> list_algs; SafeMatList<T>* m_pFinishQueue; AbstrctThreadAlg(int sz, SafeMatList<T>* pre_queue): m_pReadyQueue(pre_queue){ m_pFinishQueue = nullptr; if(sz > 0) m_pFinishQueue = new SafeMatList<T>(sz); } ~AbstrctThreadAlg(){ for(int i = 0; i < list_algs.size(); i++) delete list_algs[i]; if(m_pFinishQueue != nullptr) delete m_pFinishQueue; m_pFinishQueue = nullptr; } AbstrctThreadAlg(const AbstrctThreadAlg& threadAlg) = delete; AbstrctThreadAlg& operator=(const AbstrctThreadAlg& threadAlg) = delete;
void run() { if(!m_pReadyQueue) cout << "This thread does't have input data queue!" << endl; if(!m_pFinishQueue) cout << "This thread does't have out data queue!" << endl; while(1){ T input_data; if(m_pReadyQueue){ while(m_pReadyQueue->empty()); input_data = std::move(m_pReadyQueue->getItem()); } for(auto &it : list_algs) input_data = std::move(it->run(input_data));
if(m_pFinishQueue){ while(m_pFinishQueue->full()); m_pFinishQueue->pushItem(input_data); } } }; protected: SafeMatList<T>* m_pReadyQueue; };
template <typename T> class PrepocessTaskSingleton{ public: ~PrepocessTaskSingleton(){ for(auto &alg : m_pThreadAlgs) delete alg; }; void add_Algorm(int alg_id, void* p){ std::lock_guard<std::mutex> lg(mtx_); alg_pairs.push_back({alg_id, (void*)(p)}); }; void setDataSize(int i, int n){ std::lock_guard<std::mutex> lg(mtx_); if(i >= thread_num) return; data_size[i] = n; }; int getDataSize(int i){ std::lock_guard<std::mutex> lg(mtx_); if(i >= thread_num) return -1; return data_size[i]; }; SafeMatList<T>* getResultQueue(int i){ std::lock_guard<std::mutex> lg(mtx_); if(i >= m_pThreadAlgs.size()) return nullptr; return m_pThreadAlgs[i]; };
void build(SafeMatList<T>* q_in = nullptr){ std::lock_guard<std::mutex> lg(mtx_);
SafeMatList<T>* pQueue = q_in; int N = alg_pairs.size(); int per = N / thread_num + (N%thread_num ? 1 :0);
for(int i = 0; i < thread_num; i++){ vector<pair<int, void*>> p;
for(int j = i*per; j < min((i+1)*per, N); j++) p.push_back(alg_pairs[j]);
pQueue = per_threadContrust(p, pQueue, data_size[i]); } }; void run(){ std::lock_guard<std::mutex> lg(mtx_); vector<std::thread> tasks; for(auto& algs : m_pThreadAlgs) tasks.push_back(std::thread(AbstrctThreadAlg<T, void*>::run, algs)); for(auto& t : tasks) t.join(); }; static PrepocessTaskSingleton<T>* getPrepocessBuilder(int nth){ if(singleBuilder != nullptr) return singleBuilder; std::lock_guard<std::mutex> lg(mtx_); if(singleBuilder == nullptr) singleBuilder = new PrepocessTaskSingleton<T>(nth); return singleBuilder; };
private: PrepocessTaskSingleton(int n): thread_num(n) { for(int i=0; i < thread_num-1; i++) data_size.push_back(3); data_size.push_back(0); }; SafeMatList<T>* per_threadContrust(vector<pair<int, void*>>& aps, SafeMatList<T>* data_queu, int sz){ auto p = new AbstrctThreadAlg<T, void*>(sz, data_queu); m_pThreadAlgs.push_back(p); AlgorithmFactory<T, void*> algorithm_creator; for(auto &ap : aps) p->list_algs.push_back(algorithm_creator.createAlgorim(ap.first, ap.second));
return p->m_pFinishQueue; }; static std::mutex mtx_; static PrepocessTaskSingleton<T>* singleBuilder; vector<AbstrctThreadAlg<T, void*>*> m_pThreadAlgs; vector<pair<int, void*>> alg_pairs; vector<int> data_size; int thread_num;
}; #endif
|