CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2011
    Posts
    3

    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?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: C++0x thread and std::future

    Quote Originally Posted by clros View Post
    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 View Post
    std::thread myThread = new std::thread(myThreadCode);
    First of all, that statement doesn't compile. Second, there is no need for dynamic allocation.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: C++0x thread and std::future

    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    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

  6. #6
    Join Date
    Jul 2011
    Posts
    3

    Re: C++0x thread and std::future

    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:romise<_Res>:romise(const std:romise<_Res>&) [with _Res = int, std:romise<_Res> = std:romise<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:romise<int>), _ArgTypes = {std:romise<int>}, typename std::_Maybe_wrap_member_pointer<_Tp>::type = void (*)(std:romise<int>)]’|


    Why?

    (It is possible to disable the smiles?)

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: C++0x thread and std::future

    Quote Originally Posted by clros View Post
    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 or tick the 'disable smilies in text' checkbox (but use code tags for posting code, because it also preserves indentation).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Jul 2011
    Posts
    3

    Re: C++0x thread and std::future

    Quote Originally Posted by D_Drmmr View Post
    Your function takes a promise by value, but promise is not copyable. Pass by reference or r-value reference instead.
    Ok, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured