gpt4 book ai didi

c++ - Boost线程开销

转载 作者:太空狗 更新时间:2023-10-29 20:15:43 25 4
gpt4 key购买 nike

我在下面的简单程序中发现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

最佳答案

你真正想要的是一个线程池:

#include "threadpool.hpp"

int main()
{
boost::threadpool::pool threadpool(8); // I have 4 cpu's
// Might be overkill need to time
// to get exact numbers but depends on
// blocking and other factors.

for(int loop = 0;loop < 100000; ++loop)
{
// schedule 100,000 small tasks to be run in the 8 threads in the pool
threadpool.schedule(task(loop));
}

// Destructor of threadpool
// will force the main thread to wait
// for all tasks to complete before exiting
}

关于c++ - Boost线程开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com