如何使用TBB在单线程中运行一个函数

How to use TBB to run a function in a single thread

我正在尝试使用 TBB 仅使用一个线程来 运行 这段代码,但我不知道如何进行。

我读到我应该使用 tbb::task_group 但我不知道如何使用它。

void CScene::load(const std::string &_name) {
  if (!references++) {
   setName(_name);
   Resources.setCurrentScene(my_name);

   TEntityParseContext ctx;
   ctx.name = my_name;
   parseScene("data/scenes/" + my_name + ".scene", ctx);

   dynamic = ctx.dynamic_scene;
   for (CHandle e : ctx.entities_loaded) {
     entities.push_back(e);
   }

   Engine.getScriptingModule().raiseEvent(CModuleScripting::SCENE_LOADED, 
   my_name);

   Resources.setCurrentScene("system");
  }
}

顺便说一句,实体是一个std::vector<CHandle>,它是class

的私有变量

您只需将 lambda 传递给 task_group::run 函数:

CScene cs;
tbb::task_group g;

g.run([&cs]{cs.load("Name");});

// maybe do some other work here

// This function either waits or executes the lambda 
// if no other thread is executing it
g.wait();