3

I don't think this is a duplicate. I'd read the "duplicate" before I asked... and I've read it again since.

In Scott Meyers' Effective Modern C++, Eighth Release, Item 38, p261 Scott says:

Once created, the std::packaged_task pt can be run on a thread. (It could be run via a call to std::async, too, but if you want to run a task using std::async, there's little reason to create a std::packaged_task, because std::async does everything std::packaged_task does before it schedules the task for execution.)

"little". Not "no".

Scott is very precise. It is part of his style to use words like "almost always" or "usually" and then come back later to explain why he left the wiggle room. In this case he didn't.

So... can anyone tell me when there would be a reason to create a std::packaged_task if you want to run it with a std::async?

Rob_before_edits
  • 1,163
  • 9
  • 13
  • 2
    Can't think of any reason. To be honest the c++11 async/future infrastructure seems ill-thought out. What most of us want is asynchronous producer-consumer queues with callbacks for notification. So we're still in the position of having to write them ourselves or use boost::asio – Richard Hodges Apr 19 '16 at 22:03
  • `async` is higher level than `packaged_task`. It seems rather pointless to use both in succession, since `async` (contrary to what the name implies) does not guarantee another thread will be launched to execute the code (even if asked politely). If you go through the hoops of creating a `packaged_task`, better launch your own thread to run it, or you've basically wasted your time... – kuroi neko Apr 20 '16 at 09:46
  • "*I'd read the "duplicate" before I asked... and I've read it again since.*" So how is it that it hasn't answered your question? Because the only answer that exists is... "what does it matter?" As shown by the other question, they are separate tools for solving separate problems. Your question is like asking why you would create a `vector` then move all the elements into a `list`. – Nicol Bolas May 11 '16 at 14:58
  • Well, the "duplicate" doesn't address my question directly. Based on the information given there, and the two books I've read, I agree with the commenters above : "Can't think of any reason". Yet, SDM appears to hint that there might be a reason... and if there is one, I'd like to know it. – Rob_before_edits May 11 '16 at 21:55

0 Answers0