简介
oneTBB 的优势
- oneTBB enables you to specify logical paralleism instead of threads.
- oneTBB targets threading for performance.
- oneTBB is compatible with other threading packages.
- oneTBB emphasizes scalable, data parallel programming.
- oneTBB relies on generic programming.
并行开发中的常见问题
几种在并行开发中常见的问题:
- Interface correspondence to specification
- Memory errors
- Data race
- Race conditions and deadlocks
其中比较复杂的是:Race conditions 和 deadlocks。可以使用下面的测试方法:
- Unit tests:能力有限但基础重要
- Integration tests:组合多种功能模拟用户场景
- Stress testing:确保很少触发的错误条件也能被捕获
安装
# Clone oneTBB repository
git clone https://github.com/oneapi-src/oneTBB.git
cd oneTBB
# Create binary directory for out-of-source build
mkdir build && cd build
# Configure and build
cmake .. && make -j$(nproc)
sudo make install
使用示例
#include <tbb/tbb.h>
#include <cstdio>
using namespace tbb;
int main()
{
tick_count t0 = tick_count::now();
parallel_for(0, 100, 1, [](int i) {
printf("hello tbb %d \n", i);
});
tick_count t1 = tick_count::now();
printf("use time %f \n", (t1-t0).seconds());
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(TEST)
find_package(TBB REQUIRED)
add_executable(TEST_tbb test_tbb.cpp)
target_link_libraries(TEST_tbb TBB::tbb)
参考
Previous Article← Vtune Usage
Next ArticleTBB Sample 01 →