c++ - 如何减少从一个 boost 链到另一个 boost 链的延迟

标签 c++ boost boost-asio

假设有几个boost strand share_ptr存储在一个 vector m_poStrands中。而 tJobType 是表示不同类型工作的枚举。 我发现在一条链 (JOBA) 中发布工作到调用另一条链 (JOBB) 的 onJob 的时间差约为 50 毫秒。 我想知道是否有任何方法可以减少时间差异。

void postJob(tJobType oType, UINT8* pcBuffer, size_t iSize)
{
//...
    m_poStrands[oType]->post(boost::bind(&onJob, this, oType, pcDestBuffer, iSize));
}

void onJob(tJobType oType, UINT8* pcBuffer, size_t iSize)
{
       if (oType == JOBA)
       {
       //....
         struct timeval sTV;
    gettimeofday(&sTV, 0);
    memcpy(pcDestBuffer, &sTV, sizeof(sTV));
    pcDestBuffer += sizeof(sTV);
    iSize += sizeof(sTV);

    memcpy(pcDestBuffer, pcBuffer, iSize);

         m_poStrands[JOBB]->(boost::bind(&onJob, this, JOBB, pcDestBuffer, iSize));
       }
       else if (oType == JOBB)
       {
        // get the time from buffer
        // and calculate the dime diff
        struct timeval eTV;
        gettimeofday(&eTV, 0);
       }
}

最佳答案

您的延迟可能来自 gettimeofday 之间的 memcpy。这是我在我的机器上运行的示例程序(2 ghz core 2 duo)。我得到几千纳秒。所以几微秒。我怀疑你的系统运行速度比我的慢 4 个数量级。我见过的最糟糕的运行是两次测试之一的 100 微秒。我试图使代码尽可能接近发布的代码。

#include <boost/asio.hpp>
#include <boost/chrono.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>

struct Test {
    boost::shared_ptr<boost::asio::strand>* strands;
    boost::chrono::high_resolution_clock::time_point start;
    int id;

    Test(int i, boost::shared_ptr<boost::asio::strand>* strnds)
        : id(i),
          strands(strnds)
    {
        strands[0]->post(boost::bind(&Test::callback,this,0));
    }

    void callback(int i) {
        if (i == 0) {
          start = boost::chrono::high_resolution_clock::now();
          strands[1]->post(boost::bind(&Test::callback,this,1));
        } else {
          boost::chrono::nanoseconds sec = boost::chrono::high_resolution_clock::now() - start;
          std::cout << "test " << id << " took " << sec.count() << " ns" << std::endl;
        }
    }
};

int main() {
    boost::asio::io_service io_service_;
    boost::shared_ptr<boost::asio::strand> strands[2];
    strands[0] = boost::shared_ptr<boost::asio::strand>(new boost::asio::strand(io_service_));
    strands[1] = boost::shared_ptr<boost::asio::strand>(new boost::asio::strand(io_service_));
    boost::thread t1 (boost::bind(&boost::asio::io_service::run, &io_service_));
    boost::thread t2 (boost::bind(&boost::asio::io_service::run, &io_service_));
    Test test1 (1, strands);
    Test test2 (2, strands);
    t1.join();
    t2.join();
}

关于c++ - 如何减少从一个 boost 链到另一个 boost 链的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529391/

相关文章:

c++ - -lboost_system 对 cygwin 上对 `boost::system::system_category()' 的 undefined reference 没有帮助

c++ - boost spirit : combining preparsing with keyword parser and with Nabialek trick

c++ - 套接字 - 尽可能多地读取超时

sockets - boost::asio 套接字 async_* 链

c++ - Boost Asio async_read 不会停止阅读?

c++ - 使用相对路径 Sublime Text2 命令构建

c++ - 随机数函数未正确随机化 (C++)

c++ - 将C++ IPv6字符串表示形式转换为boost::multiprecision::uint128_t

c++ - SQLite 查询返回 "unknown error"

c++ - 在 UNIX 中输出退格字符