如何在c ++中实现异步函数 [英] How to implement an asyncronous function in c++

查看:711
本文介绍了如何在c ++中实现异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在处理一个应用程序,它在进行一些函数调用时卡住了。所以我想使用异步函数,为此我想我会在一个单独的线程中调用这些函数有一些超时。如果函数挂起,线程将在超时发生后终止。但我不知道为这个产生一个单独的线程是否是一个好主意。



所以请建议我如何做到这一点或如何实现一般的异步功能。





谢谢,

Dev。

Hi ,
I m working on an application which gets stuck while making some function calls.So I thought of using an asyncronous functions and for that i thought I will call those functions in a separate thread with some timeout.So if the functions get hung, the thread will get terminated after the time out happens.But I dont know whether this is a good idea to spawn a separate thread for this.

So please suggest me how this can be done or how to implement an asyncronous function in general.


Thanks,
Dev.

推荐答案

查看这个问题 [ ^ ]。有几个有用的链接和示例
See the solutions to this question[^]. There are several useful links and examples


这是一个好主意吗?是的,现在多线程被认为是正常的和预期的,并且在一些嵌入式应用程序之外,通常不再认为启动和清理线程的开销过于昂贵。

那么问题就变成了你如何这样做,这在很大程度上取决于你正在使用的框架,库和编译器。

当前的理论说你应该使用C ++ 11标准库中的C ++ 11线程和C语言++ 11符合编译器,你就完成了。然而,没有符合C ++ 11标准的编译器,C ++ 11线程仍然更多的是规范而不是许多部分的现实,并且好运找到一个标准库,它们都正确地实现它们并记录如何使用它们。

如果失败那么你就会离开Win32线程API,或Qt Thread类或英特尔线程原语或POSIX线程,或许多其他线程助手之一的包装器和抽象层,假设你在Windows最终映射到Win32 API函数,如CreateThread,SuspendThread,GetThreadId。

当然,你可以弯曲你的C ++规范,看看我知道的.NET或WinRT线程API 。

一旦你选择了你的线程技术,除非你想要一个严重的头痛,不要尝试混合它们,并在这里寻找与线程技术相关的示例/教程CP或其他地方。请注意,如果你刚开始使用线程,那么它们是编程中最难的部分,即使很多示例程序也会有细微的缺陷。尽管能够同时做两件事是值得的: - )
Is it a good idea? Yes, these days multiple threading is considered normal and expected and the overhead of starting and cleaning up a thread is no longer considered overly expensive in general outside of some embedded applications.
The question then becomes how do you do it and that very much depends on what framework, libraries and compiler you''re using.
Current theory says you should use C++11 threads from a C++11 standard library with a C++11 conformant compiler and you''re done. However there are no C++11 conformant compilers, C++11 threads are still more of a specification than a reality in many parts and good luck finding a standard library that both implements them correctly and documents how to use them.
Failing that then you''re left with Win32 Threading API, or Qt Thread classes or Intel threading primitives or POSIX threads or one of many other threading helpers wrappers and abstraction layers which assuming you''re on Windows all map down in the end to Win32 API functions like CreateThread, SuspendThread, GetThreadId.
Then of course you could bend your specification of C++ and look at .NET or WinRT threading APIs about which I know little.
Once you''ve picked your threading technology stick to it, don''t try to mix them unless you want a serious headache, and look for examples/tutorials related to that threading tech here on CP or elsewhere. Beware if you''re just starting with threads they are the hardest part of programming, even many example programs will have subtle flaws. It''s worth the effort though to be able to do two things at once :-)


这篇关于如何在c ++中实现异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆