如何用带引号的函数指针创建std :: thread?

时间:2016-02-01 08:16:37

标签: c++ c++11

     func save(){
          // Write codes for saving data 

           let request = NSFetchRequest(entityName: "Enitity name")

        request.returnsObjectsAsFaults = false

        let   Total   = try? context.executeFetchRequest(request)



        if let wrapResults = Total {

            fetchedArray = wrapResults

        }


             }

我尝试编译上面的简单代码但由于行class taskq { public: int trigger(taskq &tq); mutex mtx; }; void func_wrapper(taskq &tq) { cout<<endl; } int taskq::trigger(taskq &tq) { thread thread(func_wrapper, tq); return 0; } 而一直遇到错误。

此代码有什么问题?

我该如何解决?

错误信息如下:

thread thread(func_wrapper, tq);

2 个答案:

答案 0 :(得分:2)

thread thread(func_wrapper, std::ref(tq));

请注意,在执行func_wrapper时,您需要确保引用仍然有效(因为我猜这样的函数使用tq)。

答案 1 :(得分:1)

std::thread thread([&tq]() {
  func_wrapper(tq);
});