C ++非阻塞ASIO运行 [英] C++ Non-Blocking ASIO Run
问题描述
我创建了一系列跟多的libcurl并通过ASIO异步下载文件和Boost功能。
I have created a series of functions that talk to Libcurl Multi and download files asynchronously via ASIO and Boost.
显然,虽然当我打电话io_service.run它阻止我的主线程在运行时。我曾试图使它非阻塞,但我的应用程序崩溃。
Obviously though when I call io_service.run it blocks my main thread when it is run. I have tried to make it non blocking but my app crashes.
我想知道什么是在后台运行这一点,在非阻塞的方式最简单,最好的方法,并调用它,当它完成一个回调函数(像你如何能做到这在JavaScript)。
I was wondering what is the simplest and best approach to running this in the background, in a non-blocking manner and have it call a call-back function when it is done (Like how you can do it in javascript).
所以,我可以只是去:
Runthisinthebackground( thingtodo, callback);
有将运行thingtodo并将结果返回到回调。但有一件事这必须使用库,比如可以在与出C ++ 11作为其设备的移动应用程序在Android和iOS上运行运行提升
It would run the thingtodo and return the result to the callback. One thing though this must use Libraries such as boost that can run on devices with out C++ 11 as its for a mobile app running on Android and iOS
推荐答案
运行 io_service对象
在另一个线程和后它的功能:
Run io_service
in another thread and post to it your functions:
asio::io_service io_service;
// give it some work, to prevent premature exit
shared_ptr<asio::io_service::work> work(new asio::io_service::work(io_service));
boost::thread t(&asio::io_service::run, &io_service);
t.detach();
//...
io_service.post(yourFunctor); // yourFunctor will be executed in the separate thread
这篇关于C ++非阻塞ASIO运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!