Click to See Complete Forum and Search --> : C++0x thread and std::future
clros
July 6th, 2011, 05:52 AM
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?
D_Drmmr
July 6th, 2011, 08:03 AM
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.
std::thread myThread = new std::thread(myThreadCode);
First of all, that statement doesn't compile. Second, there is no need for dynamic allocation.
Codeplug
July 6th, 2011, 08:06 AM
http://bartoszmilewski.wordpress.com/2009/03/03/broken-promises-c0x-futures/
gg
D_Drmmr
July 6th, 2011, 08:57 AM
http://bartoszmilewski.wordpress.com/2009/03/03/broken-promises-c0x-futures/
I don't really get the point of that article (besides explaining futures).
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.
Codeplug
July 6th, 2011, 09:32 AM
>> It is possible get an std::future<> on an object std::thread<> after the thread has already been created normally
Here’s an example how a future could be used in the caller’s 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
clros
July 6th, 2011, 03:55 PM
http://bartoszmilewski.wordpress.com/2009/03/03/broken-promises-c0x-futures/
gg
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?)
D_Drmmr
July 7th, 2011, 04:44 AM
Why?
Your function takes a promise by value, but promise is not copyable. Pass by reference or r-value reference instead.
(It is possible to disable the smiles?)
Use code tags (http://www.codeguru.com/forum/misc.php?do=bbcode#code) or tick the 'disable smilies in text' checkbox (but use code tags for posting code, because it also preserves indentation).
clros
July 8th, 2011, 07:39 AM
Your function takes a promise by value, but promise is not copyable. Pass by reference or r-value reference instead.
Ok, thanks!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.