IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Problème boost::thread_group boost::signals2


Sujet :

C++

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2009
    Messages : 23
    Points : 17
    Points
    17
    Par défaut Problème boost::thread_group boost::signals2
    Bonjour,
    Je débute avec boost pour un projet de moteur 3D.
    J'ai essayé de créer un objet hérité de thread_group,
    le but étant d'avoir un nombre maximum de threads qui tournent.
    Dès qu'une place se libère (un thread se termine) je veux avoir la possibilité
    d'en créer un autre, pour cela mon thread doit émettre un signal quant il se termine.
    Mon problème est que j'ai un blocage de l'application et je ne vois pas
    ce que cela peut-être.

    Voici le code incriminé

    #include <boost/signals2.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/thread/thread.hpp>
    #include <boost/thread/mutex.hpp>


    class ogleThreadGroup;

    /// \class ogleThreadGroupThreadFunction
    /// \brief base class for thread of thread group
    class ogleThreadGroupThreadFunction
    {
    friend class ogleThreadGroup;
    protected:
    /// \protected void run()
    /// \nrief thread run function
    virtual void run() = 0;

    /// \protected boost::signals2::signal<void()> m_endSignal
    /// \brief the end signal
    boost::signals2::signal<void()> m_endSignal;

    public:
    ogleThreadGroupThreadFunction():m_endSignal()
    {
    }

    void operator()()
    {
    run();
    m_endSignal();
    }

    ogleThreadGroupThreadFunction(const ogleThreadGroupThreadFunction &other){}
    };


    /// \class ogleThreadGroup
    /// \brief Contain a thread stack
    class ogleThreadGroup : public boost::thread_group
    {
    protected:
    /// \protected unsigned short m_stackSize
    /// \brief the siez of stack
    unsigned short m_stackSize;

    /// \protected unsigned short m_nbFreePlace
    /// \brief number of free places in stack
    unsigned short m_nbFreePlace;

    /// \protected boost::mutex m_mutex
    /// \brief access mutex to protect m_nbFreePlace
    boost::mutex m_mutex;

    /// \fn void freePlace()
    /// \brief free a thread place
    void freePlace()
    {
    m_mutex.lock();
    m_nbFreePlace++;
    m_mutex.unlock();
    }

    public:
    ogleThreadGroup(const unsigned short &size) : thread_group(),m_mutex()
    {
    m_stackSize = size;
    m_nbFreePlace = size;
    }

    /// \fn unsigned short size()
    /// \brief size of stack
    inline unsigned short size()const
    {
    return m_stackSize;
    }

    /// \fn unsigned short nbFreePlaces()
    /// \brief free places
    inline unsigned short nbFreePlaces()
    {
    unsigned short ret;
    m_mutex.lock();
    ret = m_nbFreePlace;
    m_mutex.unlock();
    return ret;
    }


    /// \fn boost::thread* createThread(FunObject threadfunc)
    /// \brief create a thread
    template<typename FunObject>
    boost::thread* createThread(FunObject *threadfunc)
    {
    boost::thread* tmpthread = NULL;
    m_mutex.lock();
    if(m_nbFreePlace > 0)
    {
    boost::shared_ptr<ogleThreadGroup> slotContainer(this);
    ogleThreadGroupThreadFunction *funObj = (ogleThreadGroupThreadFunction *)threadfunc;
    funObj->m_endSignal.connect(boost::signals2::signal<void()>::slot_type(&ogleThreadGroup::freePlace,slotContainer.get()).track(slotContainer));
    m_nbFreePlace--;
    tmpthread = create_thread(*threadfunc);
    }
    m_mutex.unlock();
    return tmpthread;
    }

    private:
    ogleThreadGroup(const ogleThreadGroup &other)
    {
    m_stackSize = other.m_stackSize;
    m_nbFreePlace = m_stackSize;
    }

    };

    class testThread : public ogleThreadGroupThreadFunction
    {
    protected:

    static boost::mutex m_mutexSecond;
    /// \protected void run()
    /// \nrief thread run function
    virtual void run()
    {
    m_mutexSecond.lock();
    std::cout<<"test1";
    m_mutexSecond.unlock();
    int i = 0;
    }

    public:
    testThread()
    {
    int i = 0;
    i++;
    }

    testThread(const testThread &other){}
    };

    boost::mutex testThread::m_mutexSecond;

    int _tmain(int argc, _TCHAR* argv[])
    {
    ogleThreadGroup group(5);
    /*for(int i = 0; i<5;i++)
    {
    group.createThread(new testThread());
    }*/
    testThread *mythread = new testThread();
    group.createThread(mythread);
    int size = group.nbFreePlaces();
    group.join_all();
    delete mythread;
    return 0;
    }


    J'espère que vous pourrez m'aider.
    Merci

  2. #2
    Membre à l'essai
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2009
    Messages : 23
    Points : 17
    Points
    17
    Par défaut
    J'ai modifié mon code, j'ai corrigé mes erreurs grossière mais
    apparament le thread ne se lance pas.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
     
    #include <boost/signals2.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/thread/thread.hpp>
    #include <boost/thread/mutex.hpp>
     
     
    class ogleThreadGroup;
     
    /// \class ogleThreadGroupThreadFunction
    /// \brief base class for thread of thread group
    class ogleThreadGroupThreadFunction
    {
    	friend class ogleThreadGroup;
    protected:
    	/// \protected void run()
    	/// \nrief thread run function
    	virtual void run() = 0;
     
    public:
    	/// \protected boost::signals2::signal<void()> m_endSignal
    	/// \brief the end signal
    	boost::signals2::signal<void()> m_endSignal;
     
    public:
    	ogleThreadGroupThreadFunction():m_endSignal()
    	{
    	}
     
    	void start()
    	{
    		run();
    		m_endSignal();
    	}
     
    	ogleThreadGroupThreadFunction(const ogleThreadGroupThreadFunction &other)
    	{
    		int i=0;
    	}
    };
     
     
    /// \class ogleThreadGroup
    /// \brief Contain a thread stack
    class ogleThreadGroup : public boost::thread_group
    {
    	class ThreadFunContainer
    	{
    	protected:
    		ogleThreadGroupThreadFunction*	m_threadFun;
     
    	public:
    		ThreadFunContainer(ogleThreadGroupThreadFunction *fun)
    		{
    			m_threadFun = fun;
    		}
     
    		ThreadFunContainer(const ThreadFunContainer &other)
    		{
    			m_threadFun = other.m_threadFun;
    		}
     
    		void operator()()
    		{
    			if(m_threadFun)
    				m_threadFun->start();
    		}
     
    		inline ogleThreadGroupThreadFunction* getFun()const
    		{
    			return m_threadFun;
    		}
    	};
    protected:
    	/// \protected unsigned short   m_stackSize
    	/// \brief the siez of stack
    	unsigned short	m_stackSize;
     
    	/// \protected unsigned short   m_nbFreePlace
    	/// \brief number of free places in stack
    	unsigned short	m_nbFreePlace;
     
    	/// \protected boost::mutex     m_mutex
    	/// \brief access mutex to protect m_nbFreePlace
    	boost::mutex	m_mutex;
     
    	/// \fn void freePlace()
    	/// \brief free a thread place
    	void freePlace()
    	{
    		m_mutex.lock();
    		m_nbFreePlace++;
    		m_mutex.unlock();
    	}
     
    public:
    	ogleThreadGroup(const unsigned short &size) : thread_group(),m_mutex()
    	{
    		m_stackSize = size;
    		m_nbFreePlace = size;
    	}
     
    	/// \fn unsigned short size()
    	/// \brief size of stack
    	inline unsigned short size()const
    	{
    		return m_stackSize;
    	}
     
    	/// \fn unsigned short nbFreePlaces()
    	/// \brief free places
    	inline unsigned short nbFreePlaces()
    	{
    		unsigned short ret;
    		m_mutex.lock();
    		ret = m_nbFreePlace;
    		m_mutex.unlock();
    		return ret;
    	}
     
     
    	/// \fn boost::thread* createThread(FunObject threadfunc)
    	/// \brief create a thread
    	inline boost::thread* createThread(ogleThreadGroupThreadFunction *threadfunc)
    	{
    		boost::thread* tmpthread = NULL;
    		m_mutex.lock();
    		if(m_nbFreePlace > 0)
    		{
    			boost::shared_ptr<ogleThreadGroup> slotContainer(this);
    			threadfunc->m_endSignal.connect(boost::signals2::signal<void()>::slot_type(&ogleThreadGroup::freePlace,slotContainer.get()).track(slotContainer));			
    			m_nbFreePlace--;
    			tmpthread = create_thread(ThreadFunContainer(threadfunc));
    		}
    		m_mutex.unlock();
    		return tmpthread;
    	}
     
    private:	
    	ogleThreadGroup(const ogleThreadGroup &other)
    	{
    		m_stackSize = other.m_stackSize;
    		m_nbFreePlace = m_stackSize;
    	}
     
    };
     
    class testThread : public ogleThreadGroupThreadFunction
    {
    protected:
     
    	static boost::mutex m_mutexSecond;
    	/// \protected void run()
    	/// \nrief thread run function
    	virtual void run()
    	{
    		m_mutexSecond.lock();
    		std::cout<<"test1";
    		m_mutexSecond.unlock();
    		int i = 0;
    	}
     
    public:
    	testThread(): ogleThreadGroupThreadFunction()
    	{
    		int i = 0;
    		i++;
     
     
    	}
     
    	testThread(const testThread &other)
    	{
    		int a = 0; 
    		a++;
    	}
    };
     
    boost::mutex testThread::m_mutexSecond;
     
     
    int _tmain(int argc, _TCHAR* argv[])
    {
    	ogleThreadGroup group(5);
    	/*for(int i = 0; i<5;i++)
    	{
    		group.createThread(new testThread());
    	}*/
    	testThread *mythread = new testThread();
    	group.createThread(mythread);
    	int size = group.nbFreePlaces();
    	group.join_all();
    	delete mythread;
    	return 0;
    }

  3. #3
    Membre émérite

    Inscrit en
    Mai 2008
    Messages
    1 014
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 1 014
    Points : 2 252
    Points
    2 252
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    inline boost::thread* createThread(ogleThreadGroupThreadFunction *threadfunc)
    {
       if(m_nbFreePlace > 0)
       {
          boost::shared_ptr<ogleThreadGroup> slotContainer(this);	
         //...	
       }
    }
    Un shared_ptr sur this est crée dans le if.
    Son compteur vaut 1.
    En sortie du if le shared_ptr est détruit, le compteur va tomber à zéro et donc l'objet pointé (this) va être détruit.
    C'est donc équivalent à faire un delete this en plein milieu d'une fonction membre...

    Je ne comprends pas l'utilité de ce shared_ptr d'ailleurs, pourquoi ne pas faire simplement threadfunc->m_endSignal.connect(boost::bind(&ogleThreadGroup::freePlace, this)); ?

    Autre chose, pourquoi faire dériver ogleThreadGroup de boost::thread_group au lieu d'en faire un membre ? Le seul utilité que je vois à cette héritage pour le moment et de semer la confusion chez l'utilisateur et de l'inciter à se planter en appelant create_thread() au lieu de createThread().

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2009
    Messages : 23
    Points : 17
    Points
    17
    Par défaut
    Merci, cette fois-ci ça fonctionne correctement.
    Pour le :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    boost::shared_ptr<ogleThreadGroup> slotContainer(this);
    threadfunc->m_endSignal.connect(boost::signals2::signal<void()>::slot_type(&ogleThreadGroup::freePlace,slotContainer.get()).track(slotContainer))
    J'ai vu ça dans un bouquin et j'ai bêtement recopier le code sans trop comprendre. Ta solution est beaucoup plus simple.
    Encore merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 07/09/2010, 18h12
  2. Problème de compilation Boost::intrusive
    Par Darktib dans le forum Boost
    Réponses: 1
    Dernier message: 03/03/2010, 14h37
  3. Problème Borland builder + boost lib (random.hpp)
    Par visodyn dans le forum C++Builder
    Réponses: 1
    Dernier message: 04/02/2008, 17h29
  4. Problème Borland builder + boost lib (random.hpp)
    Par visodyn dans le forum Boost
    Réponses: 2
    Dernier message: 04/02/2008, 17h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo