スレッドプールをブースト



Boost Thread Pool



スレッドプールをブースト

スレッドプールをブースト コンポーネントasioにあり、固定サイズのスレッドプールです。

class thread_pool : public execution_context

タイプ



名前 説明
executor_type 関数をスレッドプールに送信するために使用されるエグゼキュータ。
fork_event フォーク関連のイベント通知。

メンバー関数

名前 説明
get_executor プールに関連付けられているエグゼキュータを取得します。
参加する スレッドに参加します。
notify_fork フォーク関連のイベントをexecution_contextに通知します。
やめる スレッドを停止します。
thread_pool() 自動的に決定されたスレッド数でプールを構築します。
thread_pool(std :: size_t num_threads) 指定された数のスレッドでプールを構築します。
〜thread_pool 駆逐艦。

保護されたメンバー関数



名前 説明
破壊 コンテキスト内のすべてのサービスを破棄します。
シャットダウン コンテキスト内のすべてのサービスをシャットダウンします。

友達

名前 説明
add_service (非推奨:make_service()を使用してください。)execution_contextにサービスオブジェクトを追加します。
has_service execute_contextに指定されたサービスタイプが含まれているかどうかを確認します。
make_service サービスオブジェクトを作成し、execution_contextに追加します。
use_service 指定されたタイプに対応するサービスオブジェクトを取得します。

関数をthread_poolに送信するには、 ディスパッチ役職 または 延期 無料の機能。

#include #include #include /* Mutex lock */ 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 ) { /* Define a 4-threaded thread pool */ boost::asio::thread_pool tp( 4 ) /* Put the function into the thread pool */ for( int i=0 i<5 ++i ) { boost::asio::post( tp, my_task ) } /* Put the statement block into the thread pool */ 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() }) } /* Exit all threads */ tp.join() system('PAUSE') return 0 }

要件



ヘッダ

  • boost / asio / thread_pool.hpp

コンビニエンスヘッダー

  • boost / asio.hpp