ThreadPool using ASIO - exit thread, task failed

I am writing a ThreadPool class in C ++ using Boost ASIO. Below is the code that I have written so far:

Class threadpool

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(); } 

Work 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; } }; 

home

 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 create 20 threads, but for now I just send only one (the same) task that ioservice should perform (just in order to simplify the situation and move on 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 that my threads exit even when I submit tasks in a while loop?
  • Why is the output from JobClass, that is, the values ​​of the variables a, b and c not printed?
+4
source share
2 answers

I think this is because you create a work object in the CreateThreadPool method, which is automatically destroyed when you exit the scope β†’ in this case, io_service does not have active work and does not process your tasks.

Try creating an instance of the "work" instance of your ThreadPool class, rather than a local one in the method.

 class ThreadPoolClass { private: thread_group threadPool; asio::io_service asyncIOService; std::auto_ptr<asio::io_service::work> work_; public: }; ThreadPoolClass::ThreadPoolClass(int maxNumThreads) { _Init(); maxThreads = maxNumThreads; } void ThreadPoolClass::CreateThreadPool() { work_.reset(new asio::io_service::work(asyncIOService)); for (int i = 0; i < maxThreads; i++) { cout<<"Pushed"<<endl; threadPool.create_thread(bind(&asio::io_service::run, &asyncIOService)); } } 
+3
source

OK, I will be the first to admit that I don’t know the increase, and more specifically raise :: asio from a hole in the ground, but I know what I heard about thread pools and workgroups.

Threads that must sleep until they are notified of a new job, but if they are not configured for this, they are likely to simply end the thread process and exit it. The sign of the control signal means that it is, a pool, a dream for a reasonable period of time before sending any work, and if the pool flows end, they do not wait properly. A quick look at the boost docs docs gave this , and it could be due to your problem.

In this note, is it possible that the destructor of your pool from the main () entry point, in fact, prematurely kills your working team? I see join_all, but this stop () gives me wills. if he does what his name implies, that would explain a lot. According to the description of the stop () call from the docs:

To complete the application shutdown, you must call the stop () function of the io_service object. This will result in a call to io_service run () to return as soon as possible, rejecting incomplete operations and without allowing ready-made handlers to be sent.

This immediate closing and leaving a mention seems suspiciously familiar with your current situation.

Again, I don't know boost: asio from Adam, but if I were on this, I would check the startup configuration for boost stream objects. they probably need to adjust how to get started, how to wait, etc. There should be many examples of using boost: asio on the Internet regarding the configuration of the very thing that you describe here, namely the work team paradigm. I see boost :: asio a TON on SO, so there are probably a lot of related or related questions.

Please feel free to lower this if it is of no use, and I apologize if that is the case.

+1
source

Source: https://habr.com/ru/post/1433115/


All Articles