/// Start all io_service objects, default with non-bocked mode. void start(bool blocked = false) { if (!threads_.empty()) return; { // Lock for synchronize access to data. scoped_lock_t lock(mutex_); blocked_ = blocked; // Create additional io_service pool. for (size_t i = io_services_.size(); i < pool_init_size_; ++i) io_services_.push_back(io_service_ptr(new boost::asio::io_service)); // Release redundant io_service pool. for (size_t i = io_services_.size(); i > pool_init_size_; --i) io_services_.pop_back(); // The pool is still idle now, set to true. idle_ = true; // Start all io_service. for (size_t i = io_services_.size(); i > 0 ; --i) start_one(io_services_[i - 1]); } // If in block mode, wait for all threads to exit. if (blocked_) wait(); }
ServicePool::ServicePool(size_t servicesCount) : servicesCount(servicesCount) { for (size_t i = 0; i < servicesCount; ++i) { services.push_back(io_service_ptr(new asio::io_service())); serviceWorks.push_back(asio::io_service::work(*services.back())); } }
/// Constructor. io_service_pool(size_t pool_init_size = BAS_IO_SERVICE_POOL_INIT_SIZE, size_t pool_high_watermark = BAS_IO_SERVICE_POOL_HIGH_WATERMARK, size_t pool_thread_load = BAS_IO_SERVICE_POOL_THREAD_LOAD) : mutex_(), io_services_(), threads_(), work_(), pool_init_size_(pool_init_size), pool_high_watermark_(pool_high_watermark), pool_thread_load_(pool_thread_load), next_io_service_(0), blocked_(false), idle_(true) { BOOST_ASSERT(pool_init_size_ != 0); BOOST_ASSERT(pool_high_watermark_ >= pool_init_size_); BOOST_ASSERT(pool_thread_load_ != 0); // Create io_service pool. for (size_t i = 0; i < pool_init_size_; ++i) io_services_.push_back(io_service_ptr(new boost::asio::io_service)); }