当前位置:网站首页>Boost thread pool
Boost thread pool
2022-07-19 01:51:00 【tissar】
Boost Thread pool
Boost Thread pool Located in component asio in , Is a fixed size thread pool .
class thread_pool :
public execution_context
Types
| Name | Description |
|---|---|
| executor_type | Executor used to submit functions to a thread pool. |
| fork_event | Fork-related event notifications. |
Member Functions
| Name | Description |
|---|---|
| get_executor | Obtains the executor associated with the pool. |
| join | Joins the threads. |
| notify_fork | Notify the execution_context of a fork-related event. |
| stop | Stops 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_pool | Destructor. |
Protected Member Functions
| Name | Description |
|---|---|
| destroy | Destroys all services in the context. |
| shutdown | Shuts down all services in the context. |
Friends
| Name | Description |
|---|---|
| add_service | (Deprecated: Use make_service().) Add a service object to the execution_context. |
| has_service | Determine if an execution_context contains a specified service type. |
| make_service | Creates a service object and adds it to the execution_context. |
| use_service | Obtain 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>
/* The mutex */
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 Thread pool for threads */
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;
}
Requirements
Header
- boost/asio/thread_pool.hpp
Convenience header
- boost/asio.hpp
边栏推荐
猜你喜欢

Nmap and Nikto scanning

解决scala无法对Native进行类的初始化

每日10道面试题打卡——JVM篇

Redis suddenly slowed down?

NFT排行榜-NFT实盘最新地址:NFT排行榜.COM

windwos 下载安装OpenSSH

Database programming (MySQL) of node, adding, deleting, modifying and querying

Introduction to software vulnerability analysis (5)

Express project creation and its routing introduction

面试官问:Redis 突然变慢了如何排查?
随机推荐
Cocos Creator 3.0 基础——事件系统
What are the NFT digital collection platforms? Which platforms are worth collecting?
Swift - 泛型
Redis 突然变慢了?
Iptables and snort basic configuration
ipfs 文件持久化操作
蛟分承影,雁落忘归——袋鼠云一站式全自动化运维管家ChengYing(承影)正式开源
IPFs mount to local disk
Ansible
未成年人数字安全保护的问题与对策
js 树状图数组批量循环操作
Swift 【Class】【struct】
Boost线程池
何为“数字藏品”?
MapReduce
Express project creation and its routing introduction
Database programming (MySQL) of node, adding, deleting, modifying and querying
14:07:08 ckati failed with: signal: killed
【文献阅读】Small-Footprint Keyword Spotting with Multi-Scale Temporal Convolution
README.md添加目录