+std::shared_ptr<boost::asio::io_service> create_running_io_service()
+{
+ auto service = std::make_shared<boost::asio::io_service>();
+ // To keep the io_service::run() running although no pending async
+ // operations are posted.
+ auto work = std::make_shared<boost::asio::io_service::work>(*service);
+ auto weak_work = std::weak_ptr<boost::asio::io_service::work>(work);
+ auto thread = std::make_shared<boost::thread>([service, weak_work]
+ {
+ ensure_gpf_handler_installed_for_thread("asio-thread");
+
+ while (auto strong = weak_work.lock())
+ {
+ try
+ {
+ service->run();
+ }
+ catch (...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ }
+ }
+ });
+
+ return std::shared_ptr<boost::asio::io_service>(
+ service.get(),
+ [service, work, thread](void*) mutable
+ {
+ work.reset();
+ service->stop();
+ if (thread->get_id() != boost::this_thread::get_id())
+ thread->join();
+ else
+ thread->detach();
+ });
+}
+