Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threadpool.h #2

Open
kevin-shanghai opened this issue Jul 17, 2015 · 0 comments
Open

threadpool.h #2

kevin-shanghai opened this issue Jul 17, 2015 · 0 comments

Comments

@kevin-shanghai
Copy link
Owner

ifndef THREAD_POOL_H__

define THREAD_POOL_H__

include "BoundedBlockingQueue.h"

include

include

include

include

class ThreadPool

{

typedef std::shared_ptr<std::thread> threadPtr;

typedef std::function<void(void)> threadFuncCallback;

public:

ThreadPool(int threadNum = 10) 

          : m_iThreadNum(std::thread::hardware_concurrency())

          , m_threadFuncQueue(10)

{

    startAllThread(m_iThreadNum);

}



~ThreadPool()

{



}



void AddTask(threadFuncCallback& task)

{

    m_threadFuncQueue.Put(task);

}



void Stop()

{

    m_brunning = false;

    for (auto thd: m_threadQueue)

    {

        thd->join();

    }

}

private:

void startAllThread(int numThreads)

{

    m_brunning = true;

    for (int i=0; i<numThreads; i++)

    {

        m_threadQueue.push_back(std::make_shared<std::thread>(&ThreadPool::RunInThread, this));

    }



}



void RunInThread(void)

{

    while (m_brunning)

    {

        auto task = m_threadFuncQueue.Take();

        task();

    }

}

private:

int m_iThreadNum;

std::deque<threadPtr> m_threadQueue;

std::atomic_bool m_brunning;

BoundedBlockingQueue<threadFuncCallback> m_threadFuncQueue;

};

endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant