Boost线程开销
Boost thread Overhead
在下面的简单程序中,我发现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
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 为什么C++线程/未来开销如此之大
- Eigen & OpenMP : 由于错误共享和线程开销,没有并行化
- Boost线程开销
- 可以通过减少多线程中系统调用(互斥/信号量)的开销来真正提高性能::atomic
- shared_ptr线程安全的开销是多少?
- 在 c++ 中为多个线程创建 lambda 的开销
- c++线程创建开销大
- OpenMP代码远慢于串行内存或线程开销瓶颈
- 为什么单个线程比多个线程快,即使它们本质上具有相同的开销
- 避免线程本地存储开销(使ffmpeg YADIF可伸缩)
- c++11线程引入了什么样的开销