如何减少从一个升压链到另一个升压链的延迟

how to reduce the latency from one boost strand to another boost strand

本文关键字:另一个 延迟 一个 何减少      更新时间:2023-10-16

假设有几个boost链share_ptr存储在vector m_postrand中。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 s。下面是我在我的机器(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();
}