Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function Object Questions

Reply
Thread Tools

Function Object Questions

 
 
Andrew Brampton
Guest
Posts: n/a
 
      09-27-2006
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


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-27-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function Object,function statement,function operator alex Javascript 3 12-28-2006 02:57 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments