I am writing a ThreadPool Class in C++ using Boost ASIO. The following is the code that I have written so far:
The ThreadPool Class
using namespace std; using namespace boost; class ThreadPoolClass { private: /* The limit to the maximum number of threads to be * instantiated within this pool */ int maxThreads; /* Group of threads in the Pool */ thread_group threadPool; asio::io_service asyncIOService; void _Init() { maxThreads = 0; } public: ThreadPoolClass(); ThreadPoolClass(int maxNumThreads); ThreadPoolClass(const ThreadPoolClass& orig); void CreateThreadPool(); void RunTask(JobClass * aJob); virtual ~ThreadPoolClass(); }; ThreadPoolClass::ThreadPoolClass() { _Init(); } ThreadPoolClass::ThreadPoolClass(int maxNumThreads) { _Init(); maxThreads = maxNumThreads; } void ThreadPoolClass::CreateThreadPool() { asio::io_service::work work(asyncIOService); for (int i = 0; i < maxThreads; i++) { cout<<"Pushed"<<endl; threadPool.create_thread(bind(&asio::io_service::run, &asyncIOService)); } } void ThreadPoolClass::RunTask(JobClass * aJob) { cout<<"RunTask"<<endl; asyncIOService.post(bind(&JobClass::Run,aJob)); } ThreadPoolClass::ThreadPoolClass(const ThreadPoolClass& orig) { } ThreadPoolClass::~ThreadPoolClass() { cout<<"Kill ye all"<<endl; asyncIOService.stop(); threadPool.join_all(); }
The Job Class
using namespace std; class JobClass { private: int a; int b; int c; public: JobClass() { //Empty Constructor } JobClass(int val) { a = val; b = val - 1; c = val + 1; } void Run() { cout<<"a: "<<a<<endl; cout<<"b: "<<b<<endl; cout<<"c: "<<c<<endl; } };
Main
using namespace std; int main(int argc, char** argv) { ThreadPoolClass ccThrPool(20); ccThrPool.CreateThreadPool(); JobClass ccJob(10); cout << "Starting..." << endl; while(1) { ccThrPool.RunTask(&ccJob); } return 0; }
So, basically I am creating 20 threads, but as of now just posting only one (same) task to be run by ioservice (just to keep things simple here and get to the root cause). The following is the output when I run this program in GDB:
Pushed [New Thread 0xb7cd2b40 (LWP 15809)] Pushed [New Thread 0xb74d1b40 (LWP 15810)] Pushed [New Thread 0xb68ffb40 (LWP 15811)] Pushed [New Thread 0xb60feb40 (LWP 15812)] Pushed [New Thread 0xb56fdb40 (LWP 15813)] Pushed [New Thread 0xb4efcb40 (LWP 15814)] Pushed [New Thread 0xb44ffb40 (LWP 15815)] Pushed [New Thread 0xb3affb40 (LWP 15816)] Pushed [New Thread 0xb30ffb40 (LWP 15817)] Pushed [New Thread 0xb28feb40 (LWP 15818)] Pushed [New Thread 0xb20fdb40 (LWP 15819)] Pushed [New Thread 0xb18fcb40 (LWP 15820)] Pushed [New Thread 0xb10fbb40 (LWP 15821)] Pushed [New Thread 0xb08fab40 (LWP 15822)] Pushed [New Thread 0xb00f9b40 (LWP 15823)] Pushed [New Thread 0xaf8f8b40 (LWP 15824)] Pushed [New Thread 0xaf0f7b40 (LWP 15825)] Pushed [New Thread 0xae8f6b40 (LWP 15826)] Pushed [New Thread 0xae0f5b40 (LWP 15827)] Pushed [New Thread 0xad8f4b40 (LWP 15828)] Starting... RunTask Kill ye all [Thread 0xb4efcb40 (LWP 15814) exited] [Thread 0xb30ffb40 (LWP 15817) exited] [Thread 0xaf8f8b40 (LWP 15824) exited] [Thread 0xae8f6b40 (LWP 15826) exited] [Thread 0xae0f5b40 (LWP 15827) exited] [Thread 0xaf0f7b40 (LWP 15825) exited] [Thread 0xb56fdb40 (LWP 15813) exited] [Thread 0xb18fcb40 (LWP 15820) exited] [Thread 0xb10fbb40 (LWP 15821) exited] [Thread 0xb20fdb40 (LWP 15819) exited] [Thread 0xad8f4b40 (LWP 15828) exited] [Thread 0xb3affb40 (LWP 15816) exited] [Thread 0xb7cd2b40 (LWP 15809) exited] [Thread 0xb60feb40 (LWP 15812) exited] [Thread 0xb08fab40 (LWP 15822) exited] [Thread 0xb68ffb40 (LWP 15811) exited] [Thread 0xb74d1b40 (LWP 15810) exited] [Thread 0xb28feb40 (LWP 15818) exited] [Thread 0xb00f9b40 (LWP 15823) exited] [Thread 0xb44ffb40 (LWP 15815) exited] [Inferior 1 (process 15808) exited normally]
I have two questions:
- Why is it so that my threads are exiting, even when I am posting tasks in a while loop?
- Why is the output from JobClass i.e. the values of the variables a,b and c not getting printed?