在 tbb::p arallel_for 中使用 tbb::queueing mutex 的简单示例程序无法编译
Simple example program that uses tbb::queueing mutex inside a tbb::parallel_for does not compile
这是我玩的一个玩具示例,用于学习如何使用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&'
tbb::p arallel_for 被设计为不编译所写的示例。 它可以防止代码中的错误。 tbb::p arallel_for 的范围形式按值将函子复制到多个任务对象中。 因此,每个任务都有一个单独的互斥锁副本,因此互斥锁不会提供预期的同步。
修复代码的方法是在结构 Parallel 外部声明互斥锁,并通过指针传递它,类似于"值"的指针。
一切都是正确的,除了必须mutable
mutex
才能在锁中使用。此外,不需要释放锁。
typedef tbb::queuing_mutex Mutex;
struct Parallel
{
mutable Mutex mutex; // mutable so that we can use it in const member
std::vector<int> * values;
Parallel(std::vector<int> * values_) : values(values_) {}
// note: this is a const member
void operator()( tbb::blocked_range< unsigned int > & range ) const
{
for(unsigned int i = range.begin(); i < range.end(); ++i)
{
Mutex::scoped_lock lock(mutex); // requires non-const argument
if ( (*values)[i] > 40)
{
std::cout << "NO SCRAMBLING ALLOWED!n";
std::cout.flush();
}
// no need to release the lock: the destructor will do that for you
}
}
};
或者,当然,您可以使成员函数成为非常量函数。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 在 tbb::p arallel_for 中使用 tbb::queueing mutex 的简单示例程序无法编译