Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

packaged_task

24 views
Skip to first unread message

Doug Mika

unread,
Aug 7, 2015, 3:50:30 PM8/7/15
to
I want to create a queue of tasks to execute at a later date. I use packaged_task to create a list of tasks that I push onto the queue. However, when you create a task you do not provide the parameters for the task until it is invoked. I want to push tasks so that the parameters needed to run the tasks are already specified so that when I will invoke the task I'll be able to do it without specifying the parameters. Is there a way to create a task with the parameters passed into the task without invoking it?

I guess the other option would be to invoke the tasks straight away and push the futures onto the queue and pop futures off of the queue, but I would prefer to invoke the tasks (and I don't wish to pass and hold the needed parameters on the queue as well)?

Kalle Olavi Niemitalo

unread,
Aug 7, 2015, 4:55:45 PM8/7/15
to
Doug Mika <doug...@gmail.com> writes:

> I use packaged_task to create a list of tasks that I push onto
> the queue. However, when you create a task you do not provide
> the parameters for the task until it is invoked. I want to
> push tasks so that the parameters needed to run the tasks are
> already specified so that when I will invoke the task I'll be
> able to do it without specifying the parameters.

How about using a lambda expression and capturing the parameters
by value:

extern void f(int i);
std::queue<std::packaged_task<void()>> q1;
for (int i = 0; i < 10; ++i)
{
q1.emplace( [=]() { f(i); } );
}

Chris Vine

unread,
Aug 8, 2015, 5:32:36 AM8/8/15
to
The other mechanism that the standard offers to construct closures is
std::bind. lambda expressions are generally easier to use though,
particularly for partial function application: partial function
application requires using std::placeholders with std:bind. (This issue
does not apply to std::packaged_task of course, which requires a fully
closed-over function object when constructed.)

Chris
0 new messages