Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #555

Defect Report: packaged_task is too strongly typed

Message-ID <5095A1F0.9000407@amacapital.net> (permalink)
Newsgroups comp.std.c++
From Andy Lutomirski <luto@amacapital.net>
Subject Defect Report: packaged_task is too strongly typed
Organization Sonic.Net
Date 2012-11-04 13:42 -0600

Show all headers | View raw


packaged_task is almost perfect for use in custom thread pools, like this:

packaged_task<int> task([] { /* something */ });
future<int> result;
threadpool.submit(task);

// later on
int x = result.get();

This doesn't work well, though: the threadpool.submit() function would
like to accept packaged_task<T> for any T -- the only packaged_task
functionality that depends on the return type (as opposed to the
parameter type) is get_future, and get_future can only be called once.

This means that the submit function would need to be a template and use
some unnecessarily complicated trick to deal with different
packaged_task types.

Any number of fixes are possible.  For example, packaged_task<void,
ArgTypes...> could have a member

template<typename F>
static pair<future<R>, packaged_task<void, ArgTypes...>>
create_void(F&& f);

Alternatively, a packaged_task that has already had get_future called
could be moveable to packaged_task<void, ArgTypes...>.

A third possibility would be for packaged_task<R, ArgTypes...> to derive
from a new template that depends only on ArgTypes.  This could be
awkward and would be more likely to have backwards compatibility issues.

(I would guess the cause of this issue is that packaged_task is trying
to have a similar interface to promise, where the get_future interface
is harmless.)


-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextNext in thread | Find similar


Thread

Defect Report: packaged_task is too strongly typed Andy Lutomirski <luto@amacapital.net> - 2012-11-04 13:42 -0600
  Re: Defect Report: packaged_task is too strongly typed Daniel Krügler <daniel.kruegler@googlemail.com> - 2012-11-04 21:16 -0600
    Re: Defect Report: packaged_task is too strongly typed Andy Lutomirski <luto@amacapital.net> - 2012-11-13 12:39 -0800
      Re: Defect Report: packaged_task is too strongly typed Daniel Krügler <daniel.kruegler@googlemail.com> - 2012-11-21 11:23 -0800
      Re: Defect Report: packaged_task is too strongly typed Daniel Krügler <daniel.kruegler@googlemail.com> - 2012-11-21 11:27 -0800

csiph-web