Function Object Questions

A

Andrew Brampton

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
 
P

Pete Becker

Andrew said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top