【cpprestsdk】浅谈cpprestsdk线程池及使用

cpprestsdk根据include文件夹可以看到共包含两部分内容:
1、pplx
2、cpprest

pplx/threadpool.h源代码中创建线程池有两种方式
1、通过construct接口创建,返回一个unique_ptr<threadpool>,由调用者控制线程池的生命周期
    _ASYNCRTIMP static std::unique_ptr<threadpool> __cdecl construct(size_t num_threads);
2、通过静态接口initialize_with_threads接口,创建了一个静态的线程池对象,结束生命周期为进程退出
    static void initialize_with_threads(size_t num_threads);

如果想使用使用其中的cpprest来创建http server或者http client,
通过查看源代码,没找到上述第一种方式创建的线程池供http相关接口使用
./http/client/http_client_asio.cpp:382:        , m_pool_epoch_timer(crossplat::threadpool::shared_instance().service())
./http/client/http_client_asio.cpp:493:            conn = std::make_shared<asio_connection>(crossplat::threadpool::shared_instance().service());
./http/client/http_client_asio.cpp:521:        , m_resolver(crossplat::threadpool::shared_instance().service())
./http/client/http_client_asio.cpp:1861:            : m_duration(timeout.count()), m_state(created), m_timer(crossplat::threadpool::shared_instance().service())
./http/listener/http_server_asio.cpp:520:    auto& service = crossplat::threadpool::shared_instance().service();
./http/listener/http_server_asio.cpp:624:        auto newSocket = new ip::tcp::socket(crossplat::threadpool::shared_instance().service());
./http/listener/http_server_httpsys.cpp:375:    // and with VS2015 PPL tasks run on the threadpool.
./pplx/pplxlinux.cpp:38:    crossplat::threadpool::shared_instance().service().post(boost::bind(proc, param));

cpprest版本:2.10.16

=================================================================

根据①initialize_with_threads接口的说明(Libraries should avoid calling this function to avoid a diamond problem with multiple consumers attempting to customize the pool.),
提供的②schedule(T task)和③service()接口,如果还使用cpprest内的http功能,
推测作者的意图:对于线程池及boost::asio::io_service使用cpprestsdk内构建的对象供整个进程使用。

class threadpool
{
public:
    _ASYNCRTIMP static threadpool& shared_instance();
    _ASYNCRTIMP static std::unique_ptr<threadpool> __cdecl construct(size_t num_threads);

    virtual ~threadpool() = default;

    /// <summary>
    /// Initializes the cpprestsdk threadpool with a custom number of threads
    /// </summary>
    /// <remarks>
    /// This function allows an application (in their main function) to initialize the cpprestsdk
    /// threadpool with a custom threadcount. Libraries should avoid calling this function to avoid
    /// a diamond problem with multiple consumers attempting to customize the pool.
    /// </remarks>
    /// <exception cref="std::exception">Thrown if the threadpool has already been initialized</exception>
    static void initialize_with_threads(size_t num_threads);

    template<typename T>
    CASABLANCA_DEPRECATED("Use `.service().post(task)` directly.")
    void schedule(T task)
    {
        service().post(task);
    }

    boost::asio::io_service& service() { return m_service; }

protected:
    threadpool(size_t num_threads) : m_service(static_cast<int>(num_threads)) {}

    boost::asio::io_service m_service;
};

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 WebSocket 查看币安所有 symbol,你需要按照以下步骤操作: 1. 连接到币安的 WebSocket API。你可以使用 C++ 中的 WebSocket 库,如 Boost.Beast 或 cpprestsdk 来实现。 2. 发送一个订阅消息,订阅币安的 `!ticker@arr` 主题,这个主题将返回所有交易对的 ticker 数据。 3. 解析返回的数据,提取出每个交易对的 symbol。在币安 API 文档中,symbol 是指交易对的名称,例如 BTCUSDT。 4. 将提取出的 symbol 存储在一个数据结构中,以供你的应用程序使用。 以下是一个使用 cpprestsdk 库订阅币安 ticker 数据的示例代码: ``` #include <cpprest/ws_client.h> #include <iostream> #include <string> #include <vector> using namespace web; using namespace web::websockets::client; int main() { websocket_client client; client.connect("wss://stream.binance.com:9443/ws"); json::value subscribe_msg; subscribe_msg["method"] = json::value::string("SUBSCRIBE"); subscribe_msg["params"] = json::value::array({json::value::string("!ticker@arr")}); subscribe_msg["id"] = json::value::number(1); client.send(subscribe_msg.serialize()); std::vector<std::string> symbols; client.receive().then([&symbols] (websocket_incoming_message msg) { auto body = msg.extract_string().get(); auto ticker_data = json::value::parse(body); for (const auto& ticker : ticker_data.as_array()) { symbols.push_back(ticker["s"].as_string()); } }).wait(); std::cout << "All symbols on Binance:\n"; for (const auto& symbol : symbols) { std::cout << symbol << "\n"; } return 0; } ``` 这段代码使用 cpprestsdk 库连接到币安的 WebSocket API,并订阅了 `!ticker@arr` 主题。当收到数据时,它将解析数据,提取出每个交易对的 symbol,并将其存储在一个 vector 中。最后,它将所有 symbol 打印到控制台上。你可以根据自己的需要修改代码以更好地满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值