我发现在以下简单程序中,boost线程的开销有三个数量级的时间开销。是否有任何方法可以减少这种开销并加速fooThread()
的调用?
#include <iostream>
#include <time.h>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
typedef uint64_t tick_t;
#define rdtscll(val) do { \
unsigned int __a,__d; \
__asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
(val) = ((unsigned long long)__a) | (((unsigned long long)__d)<<32); \
} while(0)
class baseClass {
public:
void foo(){
//Do nothing
}
void threadFoo(){
threadObjOne = boost::thread(&baseClass::foo, this);
threadObjOne.join();
}
private:
boost::thread threadObjOne;
};
int main(){
std::cout<< "main startup"<<std::endl;
baseClass baseObj;
tick_t startTime,endTime;
rdtscll(startTime);
baseObj.foo();
rdtscll(endTime);
std::cout<<"native foo() call takes "<< endTime-startTime <<" clock cycles"<<std::endl;
rdtscll(startTime);
baseObj.threadFoo();
rdtscll(endTime);
std::cout<<"Thread foo() call takes "<< endTime-startTime <<" clock cycles"<<std::endl;
}
你可以使用
g++ -lboost_thread-mt main.cpp
编译它,这是我的机器上的示例输出:main startup
native foo() call takes 2187 clock cycles
Thread foo() call takes 29630434 clock cycles