简体   繁体   English

如何在不使用Future标头的情况下从线程返回值?

[英]How can I return a value from a thread without using the future header?

My current compiler doesn't support the future header, and I'm unable to update it as on Android, the compiler is wrapped in an app. 我当前的编译器不支持将来的标头,并且由于在Android上,我无法对其进行更新,因此该编译器封装在应用程序中。 Because of this, I decided to try to make my own simple future class. 因此,我决定尝试开设自己的简单未来课程。 I have most of it laid out, but can't figure out how to get the return value from the thread (as this seems to be beyond its intended use). 我已将大部分内容都布置好了,但无法弄清楚如何从线程中获取返回值(因为这似乎超出了预期的用途)。 I could pass my future class a functor that contains it's own return holder variable to be retrieved later, but that's a little limiting, because anything I wanted to pass it would have to be derived from the original functors class, and not just a function. 我可以给我的future类传递一个函子,该函子包含它自己的返回持有者变量,以供以后检索,但这是一个限制,因为我要传递的任何东西都必须派生自原始的函子类,而不仅仅是一个函数。

Is there any way to achieve this? 有什么办法可以做到这一点? I'm assuming the standard future header doesn't use black magic to do it. 我假设标准的将来标头不使用黑魔法来做到这一点。 Does it use a "different version" of threads that allow it to receive a value back? 它是否使用线程的“不同版本”以允许其返回值?

(It's not so old as to not be compatible with threading, it just has issues with the future header for some reason) (它不旧,不与线程兼容,由于某种原因,它仅与将来的标头有关)

Thanks 谢谢

I think by writing your own simple Future class you're on the right way. 我认为通过编写自己的简单Future类,您将走上正确的道路。 What does your code look like? 您的代码是什么样的?

I'm not sure if I fully understand your statement: 我不确定我是否完全理解您的声明:

I could pass my future class a functor that contains it's own return holder variable to be retrieved later, but that's a little limiting, because anything I wanted to pass it would have to be derived from the original functors class, and not just a function. 我可以给我的future类传递一个函子,该函子包含它自己的返回持有者变量,以供以后检索,但这是一个限制,因为我要传递的任何东西都必须派生自原始的函子类,而不仅仅是一个函数。

I wouldn't pass a functor but only store variables for the values I want as return values from the thread. 我不会传递函子,而只会存储我想要的值的变量作为线程的返回值。 If you want different return values you could derive classes from your SimpleFuture class. 如果您想要不同的返回值,则可以从SimpleFuture类派生类。

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

相关问题 如何在C ++中使用Future和Promise从函数线程中获取返回值 - How to get return value from function thread using future and promise in c++ 如何在不使用std :: async的情况下使用std :: future? - How can I use std::future without using std::async? 来自使用promise的线程的C ++多个返回值,未来? - C++ multiple return values from thread using promise, future? 如何在不使用C ++ / C的阻塞函数的情况下将值从线程返回到主函数 - How to return value from the thread back to main function without using blocking function in C++/C 如何制作我的QThread块并等待从主线程调用的函数返回值? - How can I make my QThread block and wait for a function called from the main thread to return a value? 如何从线程函数返回值? - How do I return a value from thread function? 如何在不使用C ++ 11线程的情况下使用functor作为线程函数在Windows中开始线程? - How can I begin thread in windows using functor as thread function without using C++11 thread? 如何在不使用 IDE 的情况下从 cpp 文件自动生成/更新头文件? - How can I automatically generate / update header files from cpp files without using an IDE? 如何从函数返回值 - How can I return a value from a function 如何在不设置值的情况下通知 C++ 未来? - How do I notify a c++ future without setting a value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM