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
Partager