首页
/ checkedthreads 技术文档

checkedthreads 技术文档

2024-12-27 08:56:00作者:龚格成

本文档将详细介绍如何安装和使用 checkedthreads,以及如何使用其提供的 API。checkedthreads 是一个针对 C 和 C++ 的 fork-join 并行框架,提供了自动的竞态条件检测、自动负载平衡以及简洁的 API。

1. 安装指南

首先,确保您的系统满足以下要求:

  • C89 或 C++11 编译器
  • Valgrind(用于 thorough verification)

然后,按照以下步骤进行安装:

git clone https://github.com/yourusername/checkedthreads.git
cd checkedthreads
make
sudo make install

确保在安装过程中没有出现错误。

2. 项目使用说明

checkedthreads 提供了两种并行执行的方式:循环并行(ctx_for)和函数调用并行(ctx_invoke)。以下是使用示例:

循环并行

ctx_for(100, [&](int i) {
    ctx_for(100, [&](int j) {
        array[i][j] = i * j;
    });
});

在这个例子中,ctx_for 将循环迭代并行化。

函数调用并行

ctx_invoke(
    [=] { quicksort(beg, mid); },
    [=] { quicksort(mid, end); }
);

在这个例子中,ctx_invoke 将两个函数调用并行化。

取消并行执行

ct_canceller* c = ct_alloc_canceller();
ctx_for(N, [&](int i) {
    if(arr[i] == 77) {
        pos_of_77 = i;
        ct_cancel(c);
    }
}, c);
ct_free_canceller(c);

在这个例子中,如果找到特定的值,将取消循环的剩余迭代。

3. 项目 API 使用文档

以下是 checkedthreads 提供的主要 API:

  • ctx_for(int count, auto func): 并行执行循环。
  • ctx_invoke(auto func1, auto func2, ...): 并行执行多个函数调用。
  • ct_alloc_canceller(): 分配一个取消器。
  • ct_cancel(ct_canceller* canceller): 使用取消器取消并行执行。
  • ct_free_canceller(ct_canceller* canceller): 释放取消器。

4. 项目安装方式

如上所述,项目的安装方式是通过 Git 克隆仓库,然后使用 makemake install 命令。请确保您的系统具备相应的编译器和依赖项。

以上是 checkedthreads 的安装指南、使用说明和 API 文档。希望这些信息能够帮助您更好地使用这个项目。

登录后查看全文
热门项目推荐