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

Author Topic: running of multiplae thtreads in sfml simultaneously  (Read 2015 times)

0 Members and 1 Guest are viewing this topic.

hammad khan

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
running of multiplae thtreads in sfml simultaneously
« on: September 18, 2013, 09:28:54 am »
i'm doing work on chat massenger and facing difficulty to run multiple threads at the same time.

here is the sample code.

void menu();
void groupChat();
void personalChat();

int main()
{
    sf::Thread thread(&menu());
    thread.launch();
}

void menu()
{
   do
   {
      cout<<"P for group chat"<<endl;
      cout<<"p for group chat"<<endl;
      cout<<"Enter choice";

      switch( choice )
     {
      case 'p':
           sf::Thread thread(&gtoupChat);
           thread.launch();
      case 'g':
           sf::Thread thread(&personalChat);
           thread.launch();
     }
   }while( choice != 'p' choice != 'g' );
}

void groupChat()
{

};

void personalChat()
{

};

in my programm
after menu when he selects a choice, next display of menu wait the termination of the selected thread.
while i want to show menu right after when a menu is selected.
plz reply soon.
« Last Edit: September 18, 2013, 09:41:00 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: running of multiplae thtreads in sfml simultaneously
« Reply #1 on: September 18, 2013, 09:43:53 am »
There are many mistakes in your program:

1. There's no "break" instruction in your switch cases.
2. You can't declare a local variable inside a switch case (it is allowed but the compiler should warn you), you must use extra braces { }.
3. Look at the documentation of the sf::Thread destructor: it blocks and waits until the thread finishes. Since you declare your sf::Thread instances locally, they will launch and immediately wait for the launched thread to finish, cancelling the benefit of using threads.
Laurent Gomila - SFML developer

 

anything