I have a thread pool where all the threads are sharing one boost::asio::io_context. The threads in thread pool are always running because main thread has io_context::run and i am using boost::asio::executor_work_guard so it never goes out of work.
There are work which gets posted to the io_context and depending on availibility of threads those work gets done. Now i want to monitor the threads (i.e. i want to check after every x interval that the threads are not stuck).
My approach to this is:
- In main thread i keep a map of threadId and state(bool)
- i make same number of post events to io_context as there are number of threads. In these post events i put sleep for very small time and then get the threadid and set it in the map. I make these post events for every t interval where t < x/n
- At x interval i check the map if all the threadIds are set and then unset the fields.
I keep on repeating this after x interval.
My Questions
- Is my approach of monitoring the threads sharing ioContext in this way any good ?
- If its not good what other approaches can i take ?