日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Boost线程池

發(fā)布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Boost线程池 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Boost線程池

Boost 線程池位于組件 asio 中,是一種固定大小的線程池。

class thread_pool :public execution_context

Types

NameDescription
executor_typeExecutor used to submit functions to a thread pool.
fork_eventFork-related event notifications.

Member Functions

NameDescription
get_executorObtains the executor associated with the pool.
joinJoins the threads.
notify_forkNotify the execution_context of a fork-related event.
stopStops the threads.
thread_pool()Constructs a pool with an automatically determined number of threads.
thread_pool(std::size_t num_threads)Constructs a pool with a specified number of threads.
~thread_poolDestructor.

Protected Member Functions

NameDescription
destroyDestroys all services in the context.
shutdownShuts down all services in the context.

Friends

NameDescription
add_service(Deprecated: Use make_service().) Add a service object to the execution_context.
has_serviceDetermine if an execution_context contains a specified service type.
make_serviceCreates a service object and adds it to the execution_context.
use_serviceObtain the service object corresponding to the given type.

To submit functions to the thread_pool, use the dispatch , post or defer free functions.

#include <iostream> #include <thread> #include <boost/asio.hpp>/* 互斥鎖 */ std::mutex mutex_iostream;void my_task( void ) {std::lock_guard<std::mutex> lg(mutex_iostream);std::cout.flush();std::cout << "This is my task." << std::endl;std::cout.flush(); }int main( int argc, const char **argv ) {/* 定義一個4線程的線程池 */boost::asio::thread_pool tp( 4 );/* 將函數(shù)投放到線程池 */for( int i=0; i<5; ++i ) {boost::asio::post( tp, my_task );}/* 將語句塊投放到線程池 */for( int i=0; i<5; ++i ) {boost::asio::post(tp,[](){std::lock_guard<std::mutex> lg( mutex_iostream );std::cout.flush();std::cout << "This is lambda task." << std::endl;std::cout.flush();});}/* 退出所有線程 */tp.join();system("PAUSE");return 0; }

Requirements

Header

  • boost/asio/thread_pool.hpp

Convenience header

  • boost/asio.hpp

總結(jié)

以上是生活随笔為你收集整理的Boost线程池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。