在tbb :: parallel_for中使用tbb :: queueing互斥锁的简单示例程序无法编译

拉米罗

这是我正在玩的玩具示例,以学习如何使用TBB。Parallel :: operator()应该并行运行,但是它有一个关键区域,一次只能由一个处理器访问,因此它打印的消息不会被打乱。我的问题是它无法编译,并且编译器消息对我没有太大帮助。我究竟做错了什么?

另外,这是否被认为是在parallel_for内部实现互斥的正确方法?

#include <iostream>
#include <vector>
#include <cmath>
#include <tbb/tbb.h>

typedef tbb::queuing_mutex Mutex;

struct Parallel
{
    Mutex mutex;
    std::vector<int> * values;

    Parallel(std::vector<int> * values_) : values(values_) {}

    void operator()( tbb::blocked_range< unsigned int > & range ) const {
        for(unsigned int i = range.begin(); i < range.end(); ++i) {
            {
                Mutex::scoped_lock lock(mutex);
                if ( (*values)[i] > 40)
                {
                    std::cout << "NO SCRAMBLING ALLOWED!\n";
                    std::cout.flush();
                }
                lock.release();
            }
        }
    }
};

int main() {
    const int someValue = 20000;

    std::vector<int> data(someValue);
    for(int i = 0; i < someValue; ++i) {
        data[i] = std::rand();
    }

    tbb::parallel_for( tbb::blocked_range<unsigned int>(0, data.size()),
                       Parallel(&data) );
}

波纹管是错误消息:

/path-to-src/main.cpp: In member function 'void Parallel::operator()(tbb::blocked_range<unsigned int>&) const':
/path-to-src/main.cpp:20:46: error: no matching function for call to 'tbb::queuing_mutex::scoped_lock::scoped_lock(const Mutex&)'
/path-to-src/main.cpp:20:46: note: candidates are:
In file included from /usr/include/tbb/tbb.h:65:0,
                 from /path-to-src/main.cpp:4:
/usr/include/tbb/queuing_mutex.h:80:9: note: tbb::queuing_mutex::scoped_lock::scoped_lock(tbb::queuing_mutex&)
/usr/include/tbb/queuing_mutex.h:80:9: note:   no known conversion for argument 1 from 'const Mutex {aka const tbb::queuing_mutex}' to 'tbb::queuing_mutex&'
/usr/include/tbb/queuing_mutex.h:77:9: note: tbb::queuing_mutex::scoped_lock::scoped_lock()
/usr/include/tbb/queuing_mutex.h:77:9: note:   candidate expects 0 arguments, 1 provided
/usr/include/tbb/queuing_mutex.h:66:11: note: tbb::queuing_mutex::scoped_lock::scoped_lock(const tbb::queuing_mutex::scoped_lock&)
/usr/include/tbb/queuing_mutex.h:66:11: note:   no known conversion for argument 1 from 'const Mutex {aka const tbb::queuing_mutex}' to 'const tbb::queuing_mutex::scoped_lock&'
拱D.罗宾逊

tbb :: parallel_for旨在不按照编写的示例进行编译。它可以防止代码中的错误。tbb :: parallel_for的范围形式通过值将仿函数复制到多个任务对象中。因此,每个任务将具有互斥量的单独副本,因此互斥量将不会提供预期的同步。

修复代码的方法是在struct Parallel外部声明互斥量,并通过一个指针将其传递,类似于“值”的指针。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

TBB :: parallel_for创建太多的类/主体副本?

来自分类Dev

TBB :: parallel_for创建太多的类/主体副本?

来自分类Dev

在TBB parallel_for循环中获取迭代器

来自分类Dev

tbb::parallel_for 是否总是利用调用线程

来自分类Dev

TBB Parallel_for count,增量变量不准确

来自分类Dev

在C中使用Intel TBB

来自分类Dev

在C中使用Intel TBB

来自分类Dev

使用C ++的TBB链接

来自分类Dev

为什么pip无法安装tbb

来自分类Dev

使用TBB并行创建向量

来自分类Dev

使用TBB parallel_for_each()减少原子计数器

来自分类Dev

在std :: vector上的tbb :: parallel_reduce

来自分类Dev

在TBB中实施MapReduce

来自分类Dev

在parallel_for(Inter TBB)上是否有类似于我们在std :: function上看到的开销?

来自分类Dev

在Raspberry Pi 2上用TBB编译OpenCV

来自分类Dev

在至强融核中使用tbb原子操作

来自分类Dev

tbb:concurrent_hash_map <K,V>:英特尔线程构建模块(TBB)的示例代码

来自分类Dev

无法将Intel TBB库与/ usr / lib中的libtbb链接

来自分类Dev

使用狙击模拟器使用英特尔 TBB 程序

来自分类Dev

使用Clang的ThreadSanitizer和TBB避免误报

来自分类Dev

OpenCV TBB IPP OpenMP功能

来自分类Dev

TBB:可能获得线程ID?

来自分类Dev

静态链接 Intel tbb 的问题

来自分类Dev

tbb :: parallel_reduce和std :: accumulate的结果不同

来自分类Dev

tbb parallel_reduce 用于 OpenMP 缩减的片段

来自分类Dev

使用 tbb::parallel_invoke 时导致分段错误的原因是什么?

来自分类Dev

如何在OSX上将gbcc与TBB一起使用?

来自分类Dev

如何使用TBB进行多线程“尾调用”递归

来自分类Dev

线程构建块(TBB)使用lambda排队任务

Related 相关文章

热门标签

归档