Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: thread in a class  (Read 3670 times)

0 Members and 1 Guest are viewing this topic.

swa870

  • Newbie
  • *
  • Posts: 7
    • View Profile
thread in a class
« on: October 23, 2013, 10:23:15 pm »
Im trying to use my class function as a tread but the thread only do this function one time... why?

doc.h
class cScreen_Game2 : public cScreen
{
protected :
   list<TJoueur> LesJoueurs;
   vector<TEnnemis*> LesEnnemis;
   sf::Thread m_thread;
   
public:
   
   cScreen_Game2(void);
   ~cScreen_Game2(void);
   virtual int Run (sf::RenderWindow &window);
   void ThreadEnnemis();
   
   
};


 

doc.cpp


cScreen_Game2::cScreen_Game2(void):m_thread(&cScreen_Game2::ThreadEnnemis, this)
{
   

}
void cScreen_Game2::ThreadEnnemis()
{
        cout << "1";

}


int cScreen_Game2::Run (sf::RenderWindow &window)
{
bool Running = true;  
m_thread.launch();
   while (Running)
    {
   
     
         window.clear();
         sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                case sf::Event::Closed:
                    return -1;
                    break;
                case sf::Event::KeyPressed: // Nous pouvons utiliser aussi KeyReleased
                    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                       return 1;
                    break;
         }
      }
}
 
« Last Edit: October 23, 2013, 11:07:23 pm by swa870 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: thread in a class
« Reply #1 on: October 23, 2013, 11:04:14 pm »
There isn't any loop in cScreen_Game2::ThreadEnnemis, so why would it cout 1 more than once?

PS: put your code inside [code=cpp][/code] tags.

swa870

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: thread in a class
« Reply #2 on: October 23, 2013, 11:11:45 pm »
thank you for yout fast answer...

A thread would not loop itself? it's the first time that im trrying to do a thread...

Well my thread is good?

should i make a loop in the function or in put thread.lauch with my main loop?

swa870

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: thread in a class
« Reply #3 on: October 23, 2013, 11:14:44 pm »
thank you... i understand now i was right... i went back to the tutorial

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: thread in a class
« Reply #4 on: October 23, 2013, 11:18:44 pm »
You should really go read up on what threads *are* if you need to ask all of those questions.  If a thread "looped on its own", how would it ever come to an end?

Long story short, there's a cost associated with starting a thread, so it's clearly better to have the thread loop on its own rather than create and destroy a new thread every frame.  Other than that, we can't tell you whether this thread is "good" without evaluating your entire program (which we won't do).

In general you should avoid threads until you really understand what they do and how to synchronize them properly, because they are very easy to screw up and won't offer any meaningful benefit to tiny programs.  And you should use C++11's std::thread over sf::Thread if at all possible.

Edit: Maybe this wasn't needed after all but I wrote it all before you posted that so not gonna delete it.

 

anything