mercredi 30 mars 2016

Handling multiple std::async calls

I have a requirement, where I need to delete thousands of files efficiently. At present, files are deleted in a sequential manner.

I want to speed up the deletions, by calling delete in an asynchronous manner, using std::async().

Current Flow:

1. Get the list of files
2. For each file call delete()

Desired Flow:

1. Get the list of files
2. For each file:
    2a. Call AsyncDelete() using std::async()
    2b. Store the future object in a vector
3. Wait for each of the deletes to be completed and then return

I will launch each of the async tasks using std::launch::async, so that it runs on a separate thread.

I have following questions:

  1. Is async() suited for workloads involving multiple tasks? Or is it better to use threads for such tasks? I read a chapter (Item 35: Prefer task-based programming to thread-based) in Scott Myer's book "Effective Modern C++", where he recommends using task based programming instead of thread-based.

  2. How costly is each "async()" call? Does it have any overhead like a thread creation overhead? I am planning to control the number of async tasks called per cycle. For e.g. if 10,000 files are to be deleted, I will call just 100 deletes per cycle, instead of spawning 10,000 async() tasks in one go. I hope the standard library implementation efficiently handles multiple async calls (for e.g. using a thread pool).

  3. future() object returned by async() exposes both get() and wait() methods. I read that, get() internally calls wait(). Is it enough to call get() on each of the futures stored in a vector?

  4. What if a get() never returns? Is it advisable to use wait_for() with a time out?

Aucun commentaire:

Enregistrer un commentaire