Function Object Questions

Discussion in 'C++' started by Andrew Brampton, Sep 27, 2006.

  1. Hi,
    I'm using Boost::thread and when constructing the thread object I need to
    pass a Function Object so that the new thread is spawned by calling this
    object's operator().
    So I have classes like this
    class NewThread {
    NewThread() {
    // Some kind of construction
    }
    void operator()() {
    // Lots of code (that will run in the thread)
    }
    }

    NewThread nt(); // Create my NewThread object
    boost::thread t( nt ); // Now start the thread, by passing my object

    Now the problem is that the boost::thread copies my nt object, meaning a
    copy constructor is called. However because of some of the members in
    NewThread I would like the object NOT to be copied!

    So I figured to stop my NewThread object from being copied, I would wrap it
    in another Function Object, which stores a reference to NewThread. I figured
    there would be this kind of object already supplied by STL, but I couldn't
    find one, so I tried to write a generic class:

    template<class T>
    struct wrapper {
    T& func;
    wrapper(T& func) : func(func) {}
    void operator()() {
    func();
    }
    };

    Now I do this:
    NewThread nt(); // Create my NewThread object
    wrapper<NewThread > wrap (nt);
    boost::thread t( wrap ); // Now start the thread, and my wrapper is the
    things that copied

    and this seems to work fine!, I am however open for suggestions to improve
    it. However when I try and remove the w variable, and just simply do this:
    NewThread nt(); // Create my NewThread object
    boost::thread t( wrapper<NewThread > wrap (nt) );

    It fails a few lines further down, with some odd compile error to do with
    boost::thread. So maybe this is a boost specific problem, or maybe what I've
    done is illegal c++, I'm not sure.

    So I guess after this longish post my questions are:
    1) Is there a STL function object that does the same job as my wrapper
    2) Am I going about this in the correct way, to use a wrapper etc.
    3) Can my wrapper be improved?
    4) Why does it fail to work when I remove the variable w and just try to
    create the object inline.

    Thanks for any help/comments.
    Andrew

    P.S If more information is needed I'll be glad to supply it
    Andrew Brampton, Sep 27, 2006
    #1
    1. Advertising

  2. Andrew Brampton

    Pete Becker Guest

    Andrew Brampton wrote:
    >
    > So I figured to stop my NewThread object from being copied, I would wrap it
    > in another Function Object, which stores a reference to NewThread. I figured
    > there would be this kind of object already supplied by STL, but I couldn't
    > find one


    What you want is std::tr1::reference_wrapper. For details, see chapter 8
    of my book, "The Standard C++ Library Extensions."

    It's also in Boost, although the last time I looked, theirs didn't
    provide a function call operator. That was added for TR1.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Sep 27, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,030
    Evan Carew
    Jan 21, 2004
  2. Ali Syed
    Replies:
    3
    Views:
    551
    Mark McIntyre
    Oct 13, 2004
  3. pkirk25
    Replies:
    50
    Views:
    1,109
    Bill Pursell
    Oct 4, 2006
  4. Replies:
    10
    Views:
    545
  5. alex
    Replies:
    3
    Views:
    527
    Richard Cornford
    Dec 28, 2006
Loading...

Share This Page