2011-12-16 12 views
2

ベクターm_poStrandsにいくつかのブーストストランドshare_ptrが保存されているとします。また、tJobTypeは異なるタイプのジョブを示すenumです。 別のストランド(JOBB)のonJobを呼び出すためにあるストランド(JOBA)にジョブをポストしてから、時間差が約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); 
     } 
} 

答えて

2

あなたの待ち時間は、おそらくあなたのgettimeofdayの間memcpy sから来ています。私のマシンで実行したプログラム例(2 ghz core 2 duo)です。私は数千ナノセカンドを得ています。だから数マイクロ秒。私はあなたのシステムが私のものよりも4桁遅いとは思っていません。私が今までに見た最悪のテストは、2つのテストのうちの1つに対して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(); 
} 
関連する問題