mercredi 22 février 2017

Async future with a callback. C++11

I have a list of futures. The problem is that I have a lot of files and I need to make some long operation after every file will be created. That's why I want to make a callback after each "file save".

E.g.,

  (new thread; saveFile 1.txt -> new thread; do a long operation after the file has been created)
  (new thread; saveFile 2.pdf -> new thread; do a long operation after the file has been created).

I need to do everything in a separate thread. The saving of the file is critical, the second task can't be run before the file will have been created. How can I do it? I have the following code:

 void save_file() {
     // preparing data...
     saving a file
   } 

   std::vector<std::future<void>> saveFileTasks;
   for (int n = 0; n < p.size(); ++n)
   {
      saveFileTasks.push_back(std::async(std::bind(&saveFile, filename)));
   }

   for (auto &e : saveFileTasks) {
      e.get();
   }

How can I make a callback in C++11 with future/promise? I am not allowed to use boost in my project.

I'm really confused, there are so much complicated examples for a very simple task. A lot of examples can't be compilable, e.g., promise.set_wait_callback doesn't exist in C++11 but many functions have been migrated to C++11. I can do it really easy if I use Python or Clojure. How can I do it with C++?

Aucun commentaire:

Enregistrer un commentaire