首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TBB重置类成员

使用TBB重置类成员
EN

Stack Overflow用户
提问于 2022-08-15 20:20:30
回答 2查看 68关注 0票数 0

我使用TBB并行地在队列中加载数据。在下面的类中,load_data方法工作得很好。我正在编写方法cancel_load,它应该取消加载过程,并重置arenatask,以便从一开始就重新启动加载过程。在加载过程停止后,我将使用clear_queue方法清除队列。

在方法cancel_load中,task.cancel工作得很好。但是,当我试图将成员arenatask重置为新实例时,在编译时会看到error: use of deleted function

有人能解释一下为什么会发生这种事吗?我没有删除task对象。我只是尝试用一个新的实例来重置它。是否有更好的方法来重置这些类成员?

代码语言:javascript
复制
#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();
    }
}

误差

代码语言:javascript
复制
/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&)'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-15 20:34:22

tbb::task_arenatbb::task_group对象是不可复制的,就像您可能从std::unique_ptr那里知道的那样。这是通过已删除的operator=实现的,它解释了编译器的错误消息。

您必须以另一种方式重新初始化您的成员,或者使用指针来实例化它们的就地替换。

在您的场景中,我假设task.cancel()arena.terminate()应该完成这项工作。

票数 1
EN

Stack Overflow用户

发布于 2022-08-15 21:58:09

基于@ypnos关于使用唯一指针的注释,我想出了下面的解决方案。

代码语言:javascript
复制
#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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73366056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档