PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Problem with threads
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with threads

Réponse
 
LinkBack Outils de la discussion
Vieux 24/02/2008, 09h31   #1
yatko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with threads

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 ,
suggestion will be appreciated.


Thanks
yatko

  Réponse avec citation
Vieux 24/02/2008, 13h34   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09803 seconds with 10 queries