简体   繁体   English

从线程执行的函数返回结构数组

[英]return an array of structs from a function executed by a thread

I have an array of structure of 2 strings which I return from a function. 我有一个从函数返回的2个字符串的结构数组。 But this function is called as part of the callable function of a thread 但是此函数被称为线程的可调用函数的一部分

struct mystruct* myfunc(char* param1, int param2, int param3);

std::thread t1(myfunc, param1, param2, param3);

I understood from the std::thread documentation that the return value from myfunc will be ignored. 我从std :: thread文档中了解到,myfunc的返回值将被忽略。 But I need this data. 但是我需要这些数据。 Is there a way to get this data? 有没有办法获取这些数据? I read there is something like std::promise and std::future but really couldn't understand what they are. 我读到有一些类似std :: promise和std :: future的东西,但是真的不明白它们是什么。 Can anyone help me out with a simple example to achieve this? 有人可以帮我举一个简单的例子来实现这一目标吗?

Thanks a lot, in advance. 非常感谢,提前。

Esash 埃萨什

As you said, the proper way to do this is probably using std::future and std::async . 如您所说,执行此操作的正确方法可能是使用std::futurestd::async As cppreferece sais: 正如cppreferece所说:

The class template std::future provides a mechanism to access the result of asynchronous operations 类模板std :: future提供了一种机制来访问异步操作的结果

This is where std::async comes in. The function myfunc will be launched using std::async , which will return a "future" value (which we will catch later). 这就是std::async进入的地方。函数myfunc将使用std::async启动,它将返回一个“ future”值(稍后我们将进行介绍)。 Once you have the asynchronous function launched, you just have to ask your std::future variable to get that return value whenever it's ready. 一旦启动了异步函数,您只需要询问std::future变量就可以在准备就绪时获取该返回值。 Your code would look something like this: 您的代码如下所示:

// This will call myfunc asynchronously and assign its future return value to my_fut
std::future<mystruct*> my_fut = std::async(myfunc, param1, param2, param3);

/* 
   Do some work
*/

// We are ready to assign the return value to a variable, so we ask
// my_fut to get that "future" value we were promised.
mystruct* mptr = my_fut.get();

I think that's all you need. 我认为这就是您所需要的。

As @Fransisco Callego Salido already said the only way to do what you want is to use std::async, but be careful std::async does not guarantee that your function will be run asynchronously. 正如@Fransisco Callego Salido所说的那样,执行所需操作的唯一方法是使用std :: async,但要小心std :: async不保证您的函数将异步运行。 As cppreference says. 正如cppreference所说。

The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool) and returns a std::future that will eventually hold the result of that function call. 模板函数async异步运行函数f(可能在单独的线程中,该线程可能是线程池的一部分),并返回std :: future,该变量最终将保存该函数调用的结果。

In order to run myfunc asynchronously you have to pass another parameter to the constructor of std::async, the policy. 为了异步运行myfunc,您必须将另一个参数传递给策略std :: async的构造函数。 Right now there are 3 policies 目前有3条政策

  • std::launch::async - Guarantees that you function will be run on a new thread. std :: launch :: async-保证您的功能将在新线程上运行。
  • std::launch::deferred - Your function will be called on the current thread when you decide the call the member function of the returned future. std :: launch :: deferred-当您决定调用返回的future的成员函数时,将在当前线程上调用您的函数。
  • std::launch::async | std :: launch :: async | std::launch::deferred - It's up to the implementation to decide if you function will be run on a new thread. std :: launch :: deferred-由实现决定是否在新线程上运行函数。

Another thing to remember is, that the policy is always passed as the first parameter to std::async's constructor, if you forget to pass it the std::launch::async | 要记住的另一件事是,如果忘记将std :: launch :: async |传递给std :: async的构造函数,则该策略总是作为第一个参数传递给它。 std::launch:deferred policy will be used! std :: launch:将使用延迟策略! So in order guarantee an that your function is executed on a new thread you have to call it like this. 因此,为了确保您的函数在新线程上执行,您必须像这样调用它。

std::future<mystruct*> myfunc_future=std::async(std::launch::async, myfunc, param1, param2, param3);
mystruct* myfunc_result=myfunc.get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM