Created
June 23, 2014 22:06
-
-
Save mpapierski/0681ff4566a59c9e3938 to your computer and use it in GitHub Desktop.
boost.asio and custom coroutines experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <future> | |
#include <boost/optional.hpp> | |
#include <boost/filesystem.hpp> | |
#include <boost/thread.hpp> | |
#include <boost/asio.hpp> | |
#include <boost/asio/use_future.hpp> | |
#include <boost/asio/spawn.hpp> | |
struct scheduler | |
: boost::asio::io_service::service | |
{ | |
static boost::asio::io_service::id id; | |
boost::asio::io_service io_service_tasks; | |
boost::optional<boost::asio::io_service::work> io_service_work; | |
boost::thread tasks_thread; | |
scheduler(boost::asio::io_service & io_service) | |
: boost::asio::io_service::service(io_service) | |
, io_service_tasks() | |
{ | |
} | |
void shutdown_service() | |
{ | |
io_service_work.reset(); | |
tasks_thread.join(); | |
} | |
void start() | |
{ | |
io_service_work = boost::in_place(boost::ref(io_service_tasks)); | |
tasks_thread = boost::thread(boost::bind(&scheduler::thread_func, this)); | |
} | |
void thread_func() | |
{ | |
std::cout << "start scheduler" << std::endl; | |
std::size_t total = io_service_tasks.run(); | |
std::cout << "stop scheduler total tasks " << total << std::endl; | |
} | |
template <typename T> | |
struct async_callback | |
{ | |
T t_; | |
async_callback(T t) : t_(t) {} | |
void operator()(const boost::system::error_code & ec) | |
{ | |
t_(ec); | |
} | |
}; | |
template <typename Op, typename Callback> | |
void schedule(Op op, boost::asio::basic_yield_context<Callback> callback) | |
{ | |
io_service_tasks.post( | |
[=]() | |
{ | |
std::cout << "schedule work in background..." << std::endl; | |
boost::system::error_code ec; | |
//op(ec); | |
std::cout << "scheduled work is done..." << std::endl; | |
//callback(ec); | |
//this->get_io_service().post(boost::bind(callback, ec)); | |
}); | |
} | |
}; | |
boost::asio::io_service::id scheduler::id; | |
void handler(boost::asio::io_service & io_service, | |
boost::asio::yield_context yield) | |
{ | |
boost::system::error_code ec; | |
boost::asio::use_service<scheduler>(io_service) | |
.schedule([](boost::system::error_code & ec) { boost::filesystem::create_directories("/tmp/boostfs", ec); }, yield[ec]); | |
std::cout << "handler done" << std::endl; | |
// boost::system::error_code ec; | |
// boost::asio::deadline_timer timer(io_service); | |
// timer.expires_from_now(boost::posix_time::seconds(1)); | |
} | |
int | |
main() | |
{ | |
boost::asio::io_service io_service; | |
boost::asio::use_service<scheduler>(io_service).start(); | |
boost::asio::spawn(io_service, | |
boost::bind(&handler, boost::ref(io_service), _1)); | |
io_service.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment