By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Function Object Questions

P: n/a
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
Sep 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Sep 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.