0

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 ?

Community
  • 1
  • 1
user3668129
  • 4,318
  • 6
  • 45
  • 87

0 Answers0