Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ThreadPool
//Title of this code #include <iostream> #include <thread> #include <list> #include <queue> #include <mutex> using namespace std; /* * JRT - JobReturnType * JIT - JobInputTpye * */ template<class JRT, class JIT> class ThreadPool { thread TPThread; list<thread> threads; queue<JIT> jobs; queue<JRT> results; mutex resultLock; mutex inputLock; JRT(*jobFunc)(JIT); int threadNum; volatile bool stop; public: ~ThreadPool() { } ThreadPool(JRT(*jFunc)(JIT), int tNum) { threadNum = tNum; jobFunc = jobF; for (int i = 0; i < threadNum; ++i) { //threads.push_back(job); } stop = false; TPThread(this->work); TPThread.join(); } JIT getJobForWork() { inputLock.lock(); JIT jit = jobs.front(input); jobs.pop(); inputLock.unlock(); return jit; } void addJobForWork(JIT input) { inputLock.lock(); jobs.push(input); inputLock.unlock(); } JRT getResult() { resultLock.lock(); JIT jit = jobs.front(input); jobs.pop(); resultLock.unlock(); return jit; } void addResult(JRT result) { resultLock.lock(); results.push(result); resultLock.unlock(); } void work() { while(!stop) { } } }; int main() { std::cout << "Hello, world!\n"; }
run
|
edit
|
history
|
help
0
Sort array of 0's and 1's
Dar
Empty C++
KhadijahAlshehhi
Anagram finder
FuktExam.cpp
LRUCache
a
a
role of copy constructor