Boost threads and overloaded call operator

P

pauldepstein

I ran the following code which outputs two lines of text on the
screen. I can understand why the line x(); outputs a line of text
because that's the normal form of the overloaded call operator
operator()()

But it's puzzling to me why the lines boost::thread t((x)); t.join
(); also output a line of text on the screen.
To me the line boost::thread t((x)) doesn't seem to be applying the
overloaded call operator.
I would have thought that the overloaded call operator is implemented
via x(); or via x.operator();

I'd be grateful if someone could explain why boost::thread t((x));
apparently implements the overloaded call operator.

Thank you very much,

Paul Epstein


#include <boost\thread\thread.hpp>
#include <iostream>

class SayHello
{
public:
void operator()()
{
std::cout<<"I expected this line to occur once, not
twice!"<<std::endl;
}

};

int main()
{ SayHello x;
boost::thread t((x));
t.join();
x();
}
 
A

Alf P. Steinbach

* (e-mail address removed):
I ran the following code which outputs two lines of text on the
screen. I can understand why the line x(); outputs a line of text
because that's the normal form of the overloaded call operator
operator()()

But it's puzzling to me why the lines boost::thread t((x)); t.join
(); also output a line of text on the screen.
To me the line boost::thread t((x)) doesn't seem to be applying the
overloaded call operator.
I would have thought that the overloaded call operator is implemented
via x(); or via x.operator();

I'd be grateful if someone could explain why boost::thread t((x));
apparently implements the overloaded call operator.

Thank you very much,

Paul Epstein


#include <boost\thread\thread.hpp>
#include <iostream>

class SayHello
{
public:
void operator()()
{
std::cout<<"I expected this line to occur once, not
twice!"<<std::endl;
}

};

int main()
{ SayHello x;
boost::thread t((x));
t.join();
1.




2.


}


Cheers & hth.,

- Alf
 
P

pauldepstein

* (e-mail address removed):










Cheers & hth.,

- Alf

Sorry Alf, but I still don't understand.
If you omit the x(); line, I don't understand how the operator()() is
being called.
Please could you explain why the first three lines of code implement
the operator()()

Thanks,

Paul
 
A

Alf P. Steinbach

* (e-mail address removed):
Sorry Alf, but I still don't understand.
If you omit the x(); line, I don't understand how the operator()() is
being called.
Please could you explain why the first three lines of code implement
the operator()()

The code doesn't implement the operator, it calls the operator.

It's what a Boost thread does, like a Java thread calls run() or whatever.

Simply don't create any threads if you don't want anything done in threads.


Cheers & hth.,

- Alf
 
S

SG

#include <boost\thread\thread.hpp>
#include <iostream>

class SayHello
{
public:
    void operator()()
    {
         std::cout<<"I expected this line to occur once, "
"not twice!"<<std::endl;
    }
};

int main()
{   SayHello x;
    boost::thread t((x));
    t.join();
    x();
}

Why do you expect this line to occur only once? You pass the functor
to the thread object which spaws a thread and runs it inside this
thread and you have a "x();" in your main function which also runs the
operator() function.

Cheers!
SG
 
B

Bart van Ingen Schenau

I ran the following code which outputs two lines of text on the
screen. I can understand why the line x(); outputs a line of text
because that's the normal form of the overloaded call operator
operator()()

But it's puzzling to me why the lines boost::thread t((x));
t.join
(); also output a line of text on the screen.
To me the line boost::thread t((x)) doesn't seem to be applying the
overloaded call operator.

That line does indeed not apply the call operator.
With the line
boost::thread t((x));
you create a new thread object and tell that object to execute the
functor x in the context of the new thread. To do this, the call
operator must be invoked on x when the new thread has been created. That
happens somewhere inside the boost library.
I would have thought that the overloaded call operator is implemented
via x(); or via x.operator();

I'd be grateful if someone could explain why boost::thread t((x));
apparently implements the overloaded call operator.

It doesn't implement the call operator, but it does invoke a previously
implemented one.
Thank you very much,

Paul Epstein
<snip - code>

Bart v Ingen Schenau
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top