最简单的TBB示例

编程入门 行业动态 更新时间:2024-10-08 23:00:03
本文介绍了最简单的TBB示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以给我一个 TBB 示例如何:

Can someone give me a TBB example how to:

  • 设置活动线程的最大计数。
  • 执行彼此独立的任务,以类的形式呈现,而不是静态函数。
  • set the maximum count of active threads.
  • execute tasks that are independent from each others and presented in the form of class, not static functions.
  • 推荐答案

    这里有几个完整的例子,使用 parallel_for ,另一个使用 parallel_for_each 。

    Here's a couple of complete examples, one using parallel_for, the other using parallel_for_each.

    更新2014-04-12 这些显示我认为是一个漂亮的老式的方式使用TBB现在;我已使用 parallel_for 使用C ++ 11 lambda。

    Update 2014-04-12: These show what I'd consider to be a pretty old fashioned way of using TBB now; I've added a separate answer using parallel_for with a C++11 lambda.

    #include "tbb/blocked_range.h" #include "tbb/parallel_for.h" #include "tbb/task_scheduler_init.h" #include <iostream> #include <vector> struct mytask { mytask(size_t n) :_n(n) {} void operator()() { for (int i=0;i<1000000;++i) {} // Deliberately run slow std::cerr << "[" << _n << "]"; } size_t _n; }; struct executor { executor(std::vector<mytask>& t) :_tasks(t) {} executor(executor& e,tbb::split) :_tasks(e._tasks) {} void operator()(const tbb::blocked_range<size_t>& r) const { for (size_t i=r.begin();i!=r.end();++i) _tasks[i](); } std::vector<mytask>& _tasks; }; int main(int,char**) { tbb::task_scheduler_init init; // Automatic number of threads // tbb::task_scheduler_init init(2); // Explicit number of threads std::vector<mytask> tasks; for (int i=0;i<1000;++i) tasks.push_back(mytask(i)); executor exec(tasks); tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec); std::cerr << std::endl; return 0; }

    #include "tbb/parallel_for_each.h" #include "tbb/task_scheduler_init.h" #include <iostream> #include <vector> struct mytask { mytask(size_t n) :_n(n) {} void operator()() { for (int i=0;i<1000000;++i) {} // Deliberately run slow std::cerr << "[" << _n << "]"; } size_t _n; }; template <typename T> struct invoker { void operator()(T& it) const {it();} }; int main(int,char**) { tbb::task_scheduler_init init; // Automatic number of threads // tbb::task_scheduler_init init(4); // Explicit number of threads std::vector<mytask> tasks; for (int i=0;i<1000;++i) tasks.push_back(mytask(i)); tbb::parallel_for_each(tasks.begin(),tasks.end(),invoker<mytask>()); std::cerr << std::endl; return 0; }

    无论是在Debian / Wheezy $ c> g ++ tbb_example.cpp -ltbb (然后使用 ./ a.out )运行

    Both compile on a Debian/Wheezy (g++ 4.7) system with g++ tbb_example.cpp -ltbb (then run with ./a.out)

    (请参阅此问题用 std :: mem_fun_ref 或替换 invoker boost :: bind )。

    (See this question for replacing that "invoker" thing with a std::mem_fun_ref or boost::bind).

    更多推荐

    最简单的TBB示例

    本文发布于:2023-11-28 08:03:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1641586.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:最简单   示例   TBB

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!