当前位置:网站首页>Boost线程池
Boost线程池
2022-07-17 00:10:00 【tissar】
Boost线程池
Boost 线程池位于组件 asio 中,是一种固定大小的线程池。
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>
/* 互斥锁 */
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 );
/* 将函数投放到线程池 */
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
边栏推荐
猜你喜欢

Classification and use of express Middleware

Punch in 10 interview questions every day - JVM article

【文献阅读】VAQF: Fully Automatic Software-Hardware Co-Design Framework for Low-Bit Vision Transformer

Mysql 安装(rpm包方式)

Iptables and snort basic configuration

Redis+Caffeine两级缓存,让访问速度纵享丝滑

VSCode中安装Go:tools failed to install.

axs火爆,还有哪些打金游戏(上)

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

The use of libtomcrypt password Library
随机推荐
typeorm mysql upsert操作
03 BTC agreement
01-BTC-密码学原理
何为“数字藏品”?
Express project creation and its routing introduction
The platform of digital collection NFT is good
Activity的启动模式
WKWebView 设置自定义UserAgent正确姿势
NameNode 和 SecondaryNameNode
NFT-数字藏品之库里为何花116万买一个NFT头像?回顾NFT头像的“发迹史”或许能找到答案
解决scala无法对Native进行类的初始化
Redis+Caffeine两级缓存,让访问速度纵享丝滑
Swift - 泛型
通感一体去蜂窝超大规模MIMO与高频段无线接入技术
同学们 小组作业记得别抄哈 幸好只传了两道题 不要出事了
【文献阅读】MCUNet: Tiny Deep Learning on IoT Devices
Today's code farmer girl did exercises on breathing lights, controlled components and high-level components
雾计算中的数据安全问题综述
温州大学X袋鼠云:高等人才教育建设,如何做到“心中有数”
基于移动互联网应用的儿童网络保护产业实践与完善