c++
How to use thread pool where each thread has its own members
for simplicity, I have an object (class) which I want to run as thread. The object has it's own members. I want to use thread pool to create at 4 threads and run those 4 threads with different parameters each time. The class (thread class) looks: class CLogicThread { public: CLogicThread(); void RunLogic(); void SetParams(int num); virtual ~CLogicThread(); private: int m_Number = 0; }; void CLogicThread::RunLogic() { m_Number = m_Number + rand() % 2; int miliToSleep = rand() % 2000 + 100; usleep(miliToSleep); std::cout << "Running Thread Id : " << std::this_thread::get_id() << " Value: " << m_Number; } void CLogicThread::SetParams(int num) { m_Number = num; } Whenever there is a thread which has finished it's job earlier, it will get a new job to perform (without creating new thread, i.e use one of the vacant threads). Something like: Pool pool(4); // I dont have this class and looks for it int counter = 0; while (true) { usleep(10); counter++; // the next code line will be blocked till new thread is avaliable CLogicThread* pLogicThread = pool->GetThreadElement(); pLogicThread->SetParams(counter); pLogicThread->RunLogic(); } I have looked here: Thread pooling in C++11 And here: CTPL but didn't found Pool with my need. Is there ant open source of Pool I can use ? or do I need to implement it ?
Related Links
tried to use thread on my server
C++ vector [] operator throws error while at() works
Clion multiple debug configurations in cmake
C++ String object wrong characters [duplicate]
Syntex of Constructor Defining
Storing a child-class pointer in a map of parent-class pointer
In a composition relationship, can a member call a parent method?
Is it use an initialize a iterator of vector with a pointer in C++ in an init function?
How Do I Make My Program in Qt Continually Send A String to My Arduino?
C++ exception int catch block executes when string exception was thrown
why does it go wrong after push_back?
How can I stop input repeating for each char inputted?
Automated build can't find boost headers, sometimes
c++/Lua - Load multiple lua file names into a c++ string[automatically]
Is there a way to request a certain location of a text file to be read in C++?
Missing vc_runtimeminimum_x86.msi and installation won't work