且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以在C ++ 11中检索线程函数的返回值吗?

更新时间:2023-02-23 08:53:49

您可以按照以下示例代码从线程获取返回值:-

You can follow this sample code to get the return value from a thread :-

int main()
{
  auto future = std::async(func_1, 2);          

  //More code later

  int number = future.get(); //Whole program waits for this

  // Do something with number

  return 0;
}

简而言之,.get()获得返回值,您可以键入并使用它.

In short, .get() gets the return value, you can typecast and use it then.