使用C 中的Boost 建立並使用執行緒池
要使用C 中的Boost 建立執行緒池,請遵循以下步驟:
boost::asio::io_service ioService; boost::thread_group threadpool;
threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioService) );
ioService.post(boost::bind(myTask, "Hello World!"));
ioService.stop();
threadpool.join_all();
// Create io_service and thread_group boost::asio::io_service ioService; boost::thread_group threadpool; // Start the ioService processing loop boost::asio::io_service::work work(ioService); // Add threads to the thread pool threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioService) ); threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioService) ); // Assign tasks to the thread pool ioService.post(boost::bind(myTask, "Hello World!")); ioService.post(boost::bind(clearCache, "./cache")); ioService.post(boost::bind(getSocialUpdates, "twitter,gmail,facebook,tumblr,reddit")); // Stop the ioService processing loop ioService.stop(); // Join all threads in the thread pool threadpool.join_all();範例程式碼:在總之,Boost 提供了一種簡單的機制來建立執行緒池並向其分配任務,從而在C 應用程式中實現並發執行。
以上是如何在 C 中建立和使用帶有 Boost 的線程池?的詳細內容。更多資訊請關注PHP中文網其他相關文章!