Zhang Jian
2 min read

TBB 入门教程

ByZhang Jian

简介

oneTBB 的优势

  1. oneTBB enables you to specify logical paralleism instead of threads.
  2. oneTBB targets threading for performance.
  3. oneTBB is compatible with other threading packages.
  4. oneTBB emphasizes scalable, data parallel programming.
  5. oneTBB relies on generic programming.

并行开发中的常见问题

几种在并行开发中常见的问题:

  1. Interface correspondence to specification
  2. Memory errors
  3. Data race
  4. Race conditions and deadlocks

其中比较复杂的是:Race conditions 和 deadlocks。可以使用下面的测试方法:

  1. Unit tests:能力有限但基础重要
  2. Integration tests:组合多种功能模拟用户场景
  3. 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 ArticleVtune Usage
Next ArticleTBB Sample 01