-3

I just learn about Drogon C++ framework, in many places it uses callback for async tasks. It is like a javascript callback long time ago. Javascript now has future.then and also the async await.

So my question is it possible using future->then for wait the result to be ready. From what I read the std::future does not have .then method. Maybe any other library that able to achieve it?

Using current callback especially for database access (query to database) are so nested.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Apin
  • 2,558
  • 2
  • 21
  • 36
  • See [std::future](https://en.cppreference.com/w/cpp/thread/future) it. has wait, wait_for and wait_until members, they stop current thread until asynchronous operation done. The you can proceed withe the flow. – Victor Gubin Sep 24 '20 at 08:55
  • @interjay he did mean that std::async looks like jscript feature, but missing method `then`. In c++ we don't have exactly that one (there was `std::then` experimental) – Swift - Friday Pie Sep 24 '20 at 11:15

1 Answers1

0

other frameworks like Qt used additional shared states to identify state of original state's owner (e.g. finished, running, etc.). C++ standard component library introduces a value of std::future_status returned by wait_for(const chrono::duration<Rep,Period>& rel_time).

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.

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, e.g 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).

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • I am not sure about blocking because blocking the coroutines means blocking the whole thread who run the coroutines. – Apin Sep 24 '20 at 12:01
  • @Apin if you create a future instance, then, like with conditional variable blocking not happen if absolute time was already reached. But still, checking shared state. But **std::async will block if you didn't saved future, because std::future::~future() will block if that's a last instance of that shared state object**. future by definition is blocking object of syncronisation. – Swift - Friday Pie Sep 24 '20 at 13:01
  • So from what I understand is that I have to save the future reference somewhere and dont destroy it until result ready. And I have another thread that loop all future and check if the future is ready and when ready i call the callback? something like that? – Apin Sep 24 '20 at 13:41
  • @Apin that's possible if wasteful, I never tried do something like that, more like I always had some fixed loop in main thread to check if results are ready and visualized it (vrendering is mostly single thread anyway). another way is probably do not use async and play with callbacks and `std::promise`. Also there was this question: https://stackoverflow.com/questions/11004273/what-is-stdpromise – Swift - Friday Pie Sep 24 '20 at 18:04