说明
线程构建模块(TBB)使您可以轻松编写并行C ++程序,这些程序可充分利用多核性能,可移植且可组合,并具有面向未来的可扩展性。
特点
广泛用于任务并行的C ++模板库。
并行算法和数据结构,可扩展的内存分配和任务调度。
丰富的通用并行功能集,C ++; Windows *,Linux *,OS X *和其他操作系统
下载
选择一个稳定版本即可,我用的是tbb-2018_U3
https://github.com/01org/tbb/releases
解压
tar -zxvf tbb-2018_U3.tar.gz
编译
cd tbb-2018_U3
make
我的编译器未报错
若编译期间报出以下错误:
/tmp/ccxNhOc9.s: Assembler messages:
/tmp/ccxNhOc9.s:615: Error: no such instruction: `xtest'
/tmp/ccxNhOc9.s:643: Error: no such instruction: `xabort $255'
/tmp/ccxNhOc9.s:652: Error: no such instruction: `xabort $255'
/tmp/ccxNhOc9.s:658: Error: no such instruction: `xend'
/tmp/ccxNhOc9.s:825: Error: no such instruction: `xbegin .L56'
/tmp/ccxNhOc9.s:988: Error: no such instruction: `xbegin .L71'
/tmp/ccxNhOc9.s:1216: Error: no such instruction: `xabort $255'
make[1]: *** [x86_rtm_rw_mutex.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory `/user_data/.tmp/linux_intel64_gcc_cc4.9.1_libc2.12_kernel2.6.32_debug'
make: *** [tbb] Error 2
则表明当前的gcc编译器不支持-mrtm。则可行的方案是注释以下几行。打开tbb目录build/linux.gcc.inc文件,注释以下几行。
# gcc 4.8 and later support RTM intrinsics, but require command line switch to enable them
ifneq (,$(shell gcc -dumpversion | egrep "^4\.[8-9]"))
RTM_KEY = -mrtm
#endif
添加tbb变量
cd build
chmod +x*.sh
sh generate_tbbvars.sh
sh tbbvars.sh
配置头文件及库文件
cd linux_intel64_gcc_你的版本_release
cp *.so /usr/lib64
cp *.so.2 /usr/lib64
ldconfig
//回到解压缩目录下
cp -r include/* /usr//include
测试
//进入解压目录下的examples目录
make
经过一段时间运行如下说明测试通过
serial run time = 0.21682
parallel run time = 0.0694263
elapsed time : 0.444736 seconds
make[1]: Leaving directory `/home/heweiwei/test/tbb-2018_U3/examples/pipeline/square'
------------------------ test_all/fibonacci/all ------------------------
make -C test_all/fibonacci -f Makefile all CXX="g++"
make[1]: Entering directory `/home/heweiwei/test/tbb-2018_U3/examples/test_all/fibonacci'
g++ -O2 -DNDEBUG -m64 -L/home/heweiwei/test/tbb-2018_U3/examples/../build/linux_intel64_gcc_cc4.8.5_libc2.17_kernel3.10.0_release -L/home/heweiwei/test/tbb-2018_U3/examples/../build/linux_intel64_gcc_cc4.8.5_libc2.17_kernel3.10.0_debug -o fibonacci Fibonacci.cpp -ltbb -lpthread -lrt -lrt
./fibonacci
TEST PASSED
make[1]: Leaving directory `/home/heweiwei/test/tbb-2018_U3/examples/test_all/fibonacci'
测试代码
#include <tbb/tbb.h>
#include<iostream>
using namespace std;
using namespace tbb;
int main()
{
int i = 0;
concurrent_queue<int> s_tbb_queue;
for (i = 10; i < 15 ; i ++)
{
s_tbb_queue.push(i);
}
concurrent_queue<int>::iterator iter;
for(iter = s_tbb_queue.unsafe_begin() ; iter != s_tbb_queue.unsafe_end() ; iter++)
{
cout<<"value="<<*iter<<endl;
}
cout<<"queue_size="<<s_tbb_queue.unsafe_size()<<endl;
int tmp = 0;
bool res = 0;
for (i = 0; i < 8 ; i++)
{
res = s_tbb_queue.try_pop(tmp);
if(res)
{
cout<<"pop_value="<<tmp<<endl;
}
else
{
cout<<"queue is empty"<<endl;
}
}
cout<<"queue_size="<<s_tbb_queue.unsafe_size()<<endl;
return 0;
}
运行
[heweiwei@heweiwei tbb_test]$ ./a.out
value=10
value=11
value=12
value=13
value=14
queue_size=5
pop_value=10
pop_value=11
pop_value=12
pop_value=13
pop_value=14
queue is empty
queue is empty
queue is empty
queue_size=0
参考
https://blog.csdn.net/u010793236/article/details/74010571
网友评论