我使用TBB并行地在队列中加载数据。在下面的类中,load_data方法工作得很好。我正在编写方法cancel_load,它应该取消加载过程,并重置arena和task,以便从一开始就重新启动加载过程。在加载过程停止后,我将使用clear_queue方法清除队列。
在方法cancel_load中,task.cancel工作得很好。但是,当我试图将成员arena和task重置为新实例时,在编译时会看到error: use of deleted function。
有人能解释一下为什么会发生这种事吗?我没有删除task对象。我只是尝试用一个新的实例来重置它。是否有更好的方法来重置这些类成员?
#include <iostream>
#include <tbb/concurrent_queue.h>
#include <tbb/task_group.h>
#include <tbb/tbb_thread.h>
class ParallelDataset {
public:
tbb::concurrent_bounded_queue<record_data::Record> MyQueue;
tbb::task_arena arena;
tbb::task_group task;
~ParallelDataset()
{
task.wait();
}
void load_() {
// start loading data into MyQueue
}
void load_data() {
arena.execute([&]() {
task.run([&]() {
load_();
});
});
}
void cancel_load() {
task.cancel();
tbb::task_arena new_arena;
tbb::task_group new_task;
arena = new_arena;
task = new_task;
std::cout << "Data loading is cancelled. Arena and Task have been reset" << std::endl;
}
void clear_queue() {
MyQueue.clear();
}
}误差
/home/src/dataset.cpp: In member function 'void ParallelDataset::cancel_load()':
/home/src/dataset.cpp:275:16: error: use of deleted function 'tbb::task_group& tbb::task_group::operator=(const tbb::task_group&)'
275 | task = new_group;
| ^~~~~~~~~
In file included from /home/src/dataset.cpp:6:
/usr/include/tbb/task_group.h:192:7: note: 'tbb::task_group& tbb::task_group::operator=(const tbb::task_group&)' is implicitly deleted because the default definition would be ill-formed:
192 | class task_group : public internal::task_group_base {
| ^~~~~~~~~~
/usr/include/tbb/task_group.h:192:7: error: use of deleted function 'tbb::internal::task_group_base& tbb::internal::task_group_base::operator=(const tbb::internal::task_group_base&)'
/usr/include/tbb/task_group.h:92:7: note: 'tbb::internal::task_group_base& tbb::internal::task_group_base::operator=(const tbb::internal::task_group_base&)' is implicitly deleted because the default definition would be ill-formed:
92 | class task_group_base : internal::no_copy {
| ^~~~~~~~~~~~~~~
/usr/include/tbb/task_group.h:92:7: error: use of deleted function 'tbb::internal::no_copy& tbb::internal::no_copy::operator=(const tbb::internal::no_copy&)'
In file included from /usr/include/tbb/task.h:23,
from /usr/include/tbb/parallel_for.h:24,
from /home/src/dataset.cpp:4:
/usr/include/tbb/tbb_stddef.h:330:7: note: 'tbb::internal::no_copy& tbb::internal::no_copy::operator=(const tbb::internal::no_copy&)' is implicitly deleted because the default definition would be ill-formed:
330 | class no_copy: no_assign {
| ^~~~~~~
/usr/include/tbb/tbb_stddef.h:330:7: error: use of deleted function 'void tbb::internal::no_assign::operator=(const tbb::internal::no_assign&)'
/usr/include/tbb/tbb_stddef.h:324:10: note: declared here
324 | void operator=( const no_assign& ) = delete;
| ^~~~~~~~
In file included from /home/src/dataset.cpp:6:
/usr/include/tbb/task_group.h:92:7: error: use of deleted function 'tbb::task_group_context& tbb::task_group_context::operator=(const tbb::task_group_context&)'
92 | class task_group_base : internal::no_copy {
| ^~~~~~~~~~~~~~~
In file included from /usr/include/tbb/parallel_for.h:24,
from /home/src/dataset.cpp:4:
/usr/include/tbb/task.h:347:7: note: 'tbb::task_group_context& tbb::task_group_context::operator=(const tbb::task_group_context&)' is implicitly deleted because the default definition would be ill-formed:
347 | class task_group_context : internal::no_copy {
| ^~~~~~~~~~~~~~~~~~
/usr/include/tbb/task.h:347:7: error: use of deleted function 'tbb::internal::no_copy& tbb::internal::no_copy::operator=(const tbb::internal::no_copy&)'发布于 2022-08-15 20:34:22
tbb::task_arena和tbb::task_group对象是不可复制的,就像您可能从std::unique_ptr那里知道的那样。这是通过已删除的operator=实现的,它解释了编译器的错误消息。
您必须以另一种方式重新初始化您的成员,或者使用指针来实例化它们的就地替换。
在您的场景中,我假设task.cancel()和arena.terminate()应该完成这项工作。
发布于 2022-08-15 21:58:09
基于@ypnos关于使用唯一指针的注释,我想出了下面的解决方案。
#include <iostream>
#include <tbb/concurrent_queue.h>
#include <tbb/task_group.h>
#include <tbb/tbb_thread.h>
class ParallelDataset {
public:
tbb::concurrent_bounded_queue<record_data::Record> MyQueue;
std::unique_ptr<tbb::task_arena> arena;
std::unique_ptr<tbb::task_group> task;
ParallelDataset () : arena(std::make_unique<tbb::task_arena>()),
task(std::make_unique<tbb::task_group>()) {}
~ParallelDataset()
{
task->wait();
}
void load_() {
// start loading data into MyQueue
}
void load_data() {
arena->execute([&]() {
task->run([&]() {
load_();
});
});
}
void cancel_load() {
task->cancel();
arena = std::move(std::make_unique<tbb::task_arena>());
task = std::move(std::make_unique<tbb::task_group>());
std::cout << "Data loading is cancelled. Arena and Task have been reset" << std::endl;
}
void clear_queue() {
MyQueue.clear();
}
}https://stackoverflow.com/questions/73366056
复制相似问题