C++0x thread and std::future
Hello,
I have a question on C++0x thread.
It is possible get an std::future<> on an object std::thread<> after the thread has already been created normally (with a simple new thread (myThreadCode))?
I'm not making my thread with std::async(), which immediately returns a std::future<>.
Example:
std::thread myThread = new std::thread(myThreadCode);
//Can I here obtain a std::future on myThread object?
Re: C++0x thread and std::future
Quote:
Originally Posted by
clros
It is possible get an std::future<> on an object std::thread<> after the thread has already been created normally (with a simple new thread (myThreadCode))?
This is what packaged_task is for. Pass your thread function to a packaged_task. Then pass the packaged_task to the thread. You can get a future from the packaged_task.
Quote:
Originally Posted by
clros
std::thread myThread = new std::thread(myThreadCode);
First of all, that statement doesn't compile. Second, there is no need for dynamic allocation.
Re: C++0x thread and std::future
Re: C++0x thread and std::future
Quote:
Originally Posted by
Codeplug
I don't really get the point of that article (besides explaining futures).
Quote:
What was omitted from the standard, however, was the ability to compose futures. Suppose you start several threads to perform calculations or retrieve data in parallel. You want to communicate with those threads using futures. And here’s the problem: you may block on any individual future but not on all of them. While you are blocked on one, other futures might become ready. Instead of spending your precious time servicing those other futures, you might be blocked on the most time-consuming one. The only option to process futures in order of completion is by polling (calling is_ready on consecutive futures in a loop) and burning the processor.
Futures model a pull-mechanism, not a push-mechanism. Calling get on a future means "I (as in, the calling thread) cannot do anything before I have this data". What's described in the article is "I want to do something whenever data is available". You can use a queue and a condition variable for that.
Re: C++0x thread and std::future
>> It is possible get an std::future<> on an object std::thread<> after the thread has already been created normally
Quote:
Originally Posted by http://bartoszmilewski.wordpress.com/2009/03/03/broken-promises-c0x-futures/
Here’s an example how a future could be used in the caller’s code:
Code:
promise<int> intPromise;
unique_future<int> intFuture = intPromise.get_future();
std::thread t(asyncFun, std::move(intPromise));
// do some other stuff
int result = intFuture.get(); // may throw MyException
>>I don't really get the point of that article (besides explaining futures).
Besides the overview, it contains an answer to the op's question.
But you're right, his complaining that the library doesn't provide "composability" is a bit weak.
gg
Re: C++0x thread and std::future
Quote:
Originally Posted by
Codeplug
This is a good example.
But, I try this program on Linux Ubuntu and GCC(G++) 4.5.2
#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::promise<int>}, typename std::_Maybe_wrap_member_pointer<_Tp>::type = void (*)(std::promise<int>)]’|
Why?
(It is possible to disable the smiles?)
Re: C++0x thread and std::future
Quote:
Originally Posted by
clros
Why?
Your function takes a promise by value, but promise is not copyable. Pass by reference or r-value reference instead.
Quote:
(It is possible to disable the smiles?)
Use code tags or tick the 'disable smilies in text' checkbox (but use code tags for posting code, because it also preserves indentation).
Re: C++0x thread and std::future
Quote:
Originally Posted by
D_Drmmr
Your function takes a promise by value, but promise is not copyable. Pass by reference or r-value reference instead.
Ok, thanks!