Problem with threads

Y

yatko

Hi all;

I want to write a class as follows:

class Foo
{
public:
Foo();
~Foo()

void Start(void);

private:
boost::thread* ptr;
void Update(int x, int y);
};

void
Foo::Start(void)
{
ptr = new boost::thread( boost::bind( &Update, x, y) ); //
compiler complains here
}

All I want to do is creating a Foo object, initializing it and
creating a thread which is calling Update() member function after
calling Start() member function. However, compiler complains and says
that

"error: ISO C++ forbids taking the address of an unqualified or
parenthesized non-static member function to form a pointer to member
function."

It seems that creating a thread that calls a member function is
impossible, and called function must be static. However, I have to
call a member function as thread, since it performs some calculation
on object's attributes. How can I solve this problem? Any help,
suggestion will be appreciated.


Thanks
yatko
 
J

James Kanze

yatko said:
I want to write a class as follows:
class Foo
{
public:
Foo();
~Foo()
void Start(void);
private:
boost::thread* ptr;
void Update(int x, int y);
};
void
Foo::Start(void)
{
ptr = new boost::thread( boost::bind( &Update, x, y) ); //
compiler complains here
}

And what is boost::thread supposed to do with &Update? Update
is a non-static member function, and can only be called on an
object. Of type Foo. The only object you're giving
boost::thread is the result of boost::bind.

And of course, you can't take the address of a member function
like that anyway; the syntax would be &Foo::Update (even in a
member function of Foo).

You need is something like:

boost::bind( &Foo::Update, this, x, y ) ;
All I want to do is creating a Foo object, initializing it
and creating a thread which is calling Update() member
function after calling Start() member function. However,
compiler complains and says that
"error: ISO C++ forbids taking the address of an unqualified
or parenthesized non-static member function to form a pointer
to member function."

Well, that's the obvious syntax error mentionned above. Once
you've resolved that, you still have to tell bind what object to
use when calling the member function.
It seems that creating a thread that calls a member function
is impossible, and called function must be static.

No, but you have to tell bind (and thus boost::thread) what
object to use. And you have to use the correct syntax for a
pointer to member.

(I modified your code so that start() took two arguments, x and
y, and with the suggested corrections, it worked for me.)
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top