Re: Problem with threads
yatko wrote:
> 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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|