简体   繁体   English

Drogon C++ 如何在不阻塞的情况下等待未来?

[英]Drogon C++ How to wait a future without blocking?

I just learn about Drogon C++ framework, in many places it uses callback for async tasks.我刚刚了解了 Drogon C++ 框架,在很多地方它使用回调来处理异步任务。 It is like a javascript callback long time ago.这就像很久以前的 javascript 回调。 Javascript now has future.then and also the async await . Javascript 现在有future.thenasync await

So my question is it possible using future->then for wait the result to be ready.所以我的问题是可以使用future->then等待结果准备好。 From what I read the std::future does not have .then method .从我读到的std::future没有.then method Maybe any other library that able to achieve it?也许任何其他图书馆能够实现它?

Using current callback especially for database access (query to database) are so nested.使用当前回调尤其是用于数据库访问(查询数据库)是如此嵌套。

other frameworks like Qt used additional shared states to identify state of original state's owner (eg finished , running , etc.).其他框架如 Qt 使用额外的共享状态来识别原始状态所有者的状态(例如finishedrunning等)。 C++ standard component library introduces a value of std::future_status returned by wait_for(const chrono::duration<Rep,Period>& rel_time) . C++ 标准组件库引入了一个由wait_for(const chrono::duration<Rep,Period>& rel_time) std::future_status wait_for(const chrono::duration<Rep,Period>& rel_time) std::future_status返回的std::future_status值。

If the shared state is not yet ready, the function blocks the calling thread and waits until it is ready or until rel_time has elapsed, whichever happens first.如果共享状态还没有准备好,该函数会阻塞调用线程并等待它准备好或直到 rel_time 已经过去,以先发生者为准。

You can set zero timeout and determine if future is ready or not.您可以设置零超时并确定未来是否准备就绪。

I'm afraid that call-back feature should be implemented as part of query, eg to make function-wrapper which would accept pointer to a callback function and call it when task is finished (a future in this case is superfluos, callback would be called in context of asynchronous thread).我担心回调功能应该作为查询的一部分来实现,例如制作函数包装器,它会接受指向回调函数的指针并在任务完成时调用它(在这种情况下,未来是多余的,回调将是在异步线程的上下文中调用)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM