This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Promise, future and thread on C++0x


On 6 July 2011 22:53, Claudio La Rosa wrote:
>
> Hello!
> I try this program on Linux Ubuntu and GCC(G++) 4.5.2 that use Promise and
> Future

(You might want to use GCC 4.6 if you're testing C++0x features, there
have been a lot of improvements to the C++0x support since GCC 4.5 was
released.)

> #include <iostream>
> #include <future>
> #include <thread>
>
> using namespace std;
>
>
> void myAsyncFun(promise<int> intPromise)
> {
> int result;
> try {
> // calculate the result
> result = 25;
> intPromise.set_value(result);
> }
> catch (std::exception& e)
> {
> intPromise.set_exception(std::copy_exception(e));
> }
> }
>
>
> int main()
> {
> promise<int> intPromise;
> future<int> intFuture = intPromise.get_future();
> std::thread t(myAsyncFun, std::move(intPromise));
> // do some other stuff
> int result = intFuture.get(); // may throw MyException
> cout<<"Result from thread: "<<result<<endl;
> return 0;
> }
>
> I get a compiler error:
>
> /usr/include/c++/4.5/future|855|error: deleted function
> ‘std:promise<_Res>:promise(const std:promise<_Res>&) [with _Res = int,
> std:promise<_Res> = std:promise<int>]’|
>
> /usr/include/c++/4.5/thread|141|error: used here|
>
> /usr/include/c++/4.5/thread|141|error: initializing argument 2 of
> ‘std::_Bind<typename std::_Maybe_wrap_member_pointer<_Tp>::type(_ArgTypes
> ...)> std::bind(_Functor, _ArgTypes ...) [with _Functor = void
> (*)(std:promise<int>), _ArgTypes = {std:romise<int>}, typename
> std::_Maybe_wrap_member_pointer<_Tp>::type = void (*)(std:promise<int>)]’|
>
>
> Why?

std::thread internally calls std::bind(myAsyncFun, args...) and then
invokes that call wrapper in a new thread.

When the bind result is invoked it passes the bound promise as an
lvalue reference to the function, but the bound function can only be
called with rvalues.

I think this is a bug in the implementation of std::thread, which
either cannot use std::bind or needs to ensure the bound arguments are
forwarded correctly.

Please report this to bugzilla, thanks.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]