ASIO正确处理多线程 + strand + socket + timer
创始人
2024-11-11 10:00:35
0

下面是一个使用ASIO正确处理多线程、strand、socket和timer的代码示例:

#include 
#include 
#include 
#include 

using namespace std;

void workerThread(asio::io_context& ioContext) {
    ioContext.run();
}

void printThreadId() {
    cout << "Thread ID: " << this_thread::get_id() << endl;
}

void printMessage(const string& message) {
    printThreadId();
    cout << message << endl;
}

void handleTimer(const asio::error_code& error, asio::steady_timer* timer) {
    if (error) {
        printMessage("Timer error: " + error.message());
        return;
    }

    printMessage("Timer expired");

    timer->expires_after(chrono::seconds(1));
    timer->async_wait([timer](const asio::error_code& error) { 
        handleTimer(error, timer);
    });
}

void handleRead(const asio::error_code& error, size_t bytesTransferred) {
    if (error) {
        printMessage("Read error: " + error.message());
        return;
    }

    printMessage("Received message: " + string(buffer.data(), bytesTransferred));
}

void startRead(asio::ip::tcp::socket& socket) {
    asio::async_read(socket, asio::buffer(buffer), asio::transfer_at_least(1),
        asio::bind_executor(strand, bind(handleRead, std::placeholders::_1, std::placeholders::_2)));
}

void handleAccept(asio::ip::tcp::socket& socket, asio::io_context& ioContext) {
    printMessage("Accepted connection");

    startRead(socket);

    asio::ip::tcp::socket newSocket(ioContext);
    acceptor.async_accept(newSocket, bind(handleAccept, ref(newSocket), ref(ioContext)));
}

int main() {
    asio::io_context ioContext;
    asio::io_context::strand strand(ioContext);

    asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), 8888);
    asio::ip::tcp::acceptor acceptor(ioContext, endpoint);

    asio::steady_timer timer(ioContext);
    timer.expires_after(chrono::seconds(1));
    timer.async_wait([&timer](const asio::error_code& error) { 
        handleTimer(error, &timer);
    });

    vector threads;
    for (int i = 0; i < thread::hardware_concurrency(); ++i) {
        threads.emplace_back([&ioContext]() {
            workerThread(ioContext);
        });
    }

    printMessage("Server started");

    asio::ip::tcp::socket socket(ioContext);
    acceptor.async_accept(socket, bind(handleAccept, ref(socket), ref(ioContext)));

    for (auto& thread : threads) {
        thread.join();
    }

    return 0;
}

在这个示例中,首先创建了一个asio::io_context对象和一个asio::io_context::strand对象。然后,创建了一个asio::ip::tcp::acceptor对象来侦听客户端连接。接下来,创建一个asio::steady_timer对象来定期触发操作。然后,创建了多个工作线程,每个线程都运行workerThread函数,其中调用ioContext.run()来执行IO操作。

handleAccept函数中,当有新的客户端连接时,会打印一条消息,并启动一个异步读操作startRead来读取客户端发送的数据。在startRead函数中,使用asio::async_read函数来异步读取数据,并使用asio::bind_executor来绑定strand对象,以确保在同一线程中处理读取操作。

handleRead函数中,当读操作完成时,会打印接收到的消息。

handleTimer函数中,当定时器超时时,会打印一条消息,并重新启动定时器。

最后,在main函数中,创建了多个工作线程,并启动ioContext.run()来执行IO操作。然后,创建一个asio::ip::tcp::socket对象,并使用acceptor.async_accept函数异步接受客户端连接。最后,等待工作线程结束,并返回0。

相关内容

热门资讯

三分钟教你!友玩广西修改器,凑... 三分钟教你!友玩广西修改器,凑一桌游戏辅助器(竟然真的有挂)-哔哩哔哩1、首先打开凑一桌游戏辅助器最...
十分钟外挂!微信小程序家乡大贰... 十分钟外挂!微信小程序家乡大贰破解版,新畅游互娱辅助,可靠教程(有挂功能)-哔哩哔哩1、全新机制【微...
六分钟关于!水鱼辅助软件下载,... 六分钟关于!水鱼辅助软件下载,蜀山四川破解版ios(一贯真的有挂)-哔哩哔哩1、起透看视 蜀山四川破...
第5分钟外挂!腾威互娱 辅助工... 第5分钟外挂!腾威互娱 辅助工具,中至赣州黑科技辅助软件视频,AA德州教程(揭秘有挂)-哔哩哔哩1、...
第二分钟解迷!多乐游戏辅助脚本... 第二分钟解迷!多乐游戏辅助脚本,好友赣南脚本插件(切实是有挂)-哔哩哔哩好友赣南脚本插件辅助器中分为...
第三分钟外挂!逍遥辅助器,天天... 第三分钟外挂!逍遥辅助器,天天爱柳州辅助器,揭秘教程(有挂总结)-哔哩哔哩1)天天爱柳州辅助器辅助挂...
1分钟解迷!新财神正版辅助挂,... 1分钟解迷!新财神正版辅助挂,四川途游辅助软件下载(原来是真的挂)-哔哩哔哩亲,关键说明,四川途游辅...
第6分钟外挂!博雅红河西元,雀... 第6分钟外挂!博雅红河西元,雀神挂件价格,规律教程(有挂秘笈)-哔哩哔哩在进入雀神挂件价格辅助挂后,...
6分钟专业!哥哥打大a有辅助吗... 6分钟专业!哥哥打大a有辅助吗,赣牌圈小程序破解版(好像有挂)-哔哩哔哩;1、玩家可以在赣牌圈小程序...
3分钟外挂!欢乐达人葫芦鱼辅助... 3分钟外挂!欢乐达人葫芦鱼辅助器,阿拉游戏中心辅助,插件教程(有挂透视)-哔哩哔哩亲,关键说明,欢乐...