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

Author Topic: problem with multiple threads in sfml  (Read 3282 times)

0 Members and 2 Guests are viewing this topic.

hammad khan

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
problem with multiple threads in sfml
« on: October 09, 2013, 10:40:07 am »
i'm working on a chat massenger, in my program i've written two threads as follows. i'm facing problem with there working. here is the main function of the program

int main()
{
        char ipAddress[20];
        getIp(ipAddress);
        sf::IpAddress ip(ipAddress);
        sf::TcpSocket *socket = new sf::TcpSocket;
        sf::Mutex mutex;
        bool done = false;

        if( socket->connect(ip, 2000) == 0 )
        {
                cout<<"Enter online id : ";
                std::getline(cin, id);
                sf::Packet packet;
                packet<<id;
                socket->send(packet);

                sf::Thread thread( &checkRequest, socket);
                thread.launch();

                mutex.lock();
                sf::Thread thread2( &menu, socket);
                thread2.launch();
                mutex.unlock();
        }
        else
        {
                cout<<"Couldn't connect to the server try again later"<<endl;
        }
        return 0;  
}
 
sometimes both threads work correctly but sometimes they don't work properly. i don't face any error. is there any problem regarding Operating system?
« Last Edit: October 09, 2013, 10:49:24 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: problem with multiple threads in sfml
« Reply #1 on: October 09, 2013, 11:17:45 am »
Mutlithread applications are not a trivial task, since you'll have to look out for all kinds of data races, deadlocks and starvation issues. Your code is incomplete, so we can't really say what's going wrong... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hammad khan

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: problem with multiple threads in sfml
« Reply #2 on: October 09, 2013, 11:54:35 am »
ok this is the whole code a little bit longer........... :-[

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <string>
#include <cstring>
#include <iostream>
#include <conio.h>
#include <vector>
#include <fstream>
using namespace std;

void getIp( char * );
void menu( sf::TcpSocket * );
void personalChat( sf::TcpSocket * );
void chatOnRequest( sf::TcpSocket * );
void chat( sf::TcpSocket * );
bool isAnyRequest( sf::TcpSocket * );
void checkRequest( sf::TcpSocket * );
string id;

int main()
{
   char ipAddress[20];
   getIp(ipAddress);
   sf::IpAddress ip(ipAddress);
   sf::TcpSocket *socket = new sf::TcpSocket;
   sf::Mutex mutex;
   bool done = false;

   if( socket->connect(ip, 2000) == 0 )
   {
      cout<<"Enter online id : ";
      std::getline(cin, id);
      sf::Packet packet;
      packet<<id;
      socket->send(packet);

      sf::Thread thread( &checkRequest, socket);
      thread.launch();

      mutex.lock();
      sf::Thread thread2( &menu, socket);
      thread2.launch();
      mutex.unlock();
   }
   else
   {
      cout<<"Couldn't connect to the server try again later"<<endl;
   }
   return 0; 
}

void getIp( char * ip )
{
   ifstream fout( "ipAddress.TXT");
   if( !fout )
      cout<<"File is not opened"<<endl;
   else
      fout.getline( ip, 20, '\n');
}

void menu( sf::TcpSocket * socket )
{
   //sf::Thread thread1( &personalChat, socket);
   char choice;
   do
   {
      cout<<"P for personal chat."<<endl;
      cout<<"Q for quit"<<endl;
      cin>>choice;

      if( toupper(choice) == 'P' )
      {
         personalChat( socket );
      }
      else if( toupper(choice) == 'Q' )
      {
         cout<<"Thanx for using messanger"<<endl;
         exit(0);
      }
      else
      {
         cout<<"this choice is not available."<<endl;
         cout<<"G for group chat."<<endl;
         cout<<"P for personal chat."<<endl;
         cout<<"Q for quit"<<endl;
         cin>>choice;
      }
   }while( toupper(choice) != 'G' || toupper(choice) != 'P' );
}

void personalChat( sf::TcpSocket * socket )
{
   socket->setBlocking(true);
   string message;
   string choice = "-10-1*0-01-p";
   sf::Packet packet;
   packet<<choice;
   socket->send(packet);
   packet<<id;
   socket->send(packet);
   packet.clear();

   if( socket->receive(packet) == sf::Socket::Done )
   {
      if( packet.getDataSize() == 0 )
      {
         cout<<"None of the other clients is online"<<endl;
         cout<<"wait...."<<endl;
      }
      else
      {
         while( !packet.endOfPacket() )
         {
            packet>>message;
            cout<<message<<endl;
         }
         packet.clear();
         string nameOfclient;
         cin.ignore();
         cout<<"With which you wanna personal chat ";
         std::getline(cin, nameOfclient);
         packet<<nameOfclient;
         socket->send(packet);
         packet>>nameOfclient;
         cout<<nameOfclient.c_str()<<endl;
         chat(socket);
      }
   }
}

void chat(sf::TcpSocket * socket )
{
   sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
   sf::Packet packet1;
   packet1<<id;
   socket->send(packet1);
   socket->setBlocking(false);
   window.setTitle(id);
   std::string text = "";
   std::vector<sf::Text> chat;
   sf::Font font;
   if( !font.loadFromFile("Candarab.ttf") )
      cout<<"can't find the file"<<endl;

   while( window.isOpen() )
   {
      sf::Event event;
      while( window.pollEvent( event ))
      {
         switch( event.type )
         {
         case sf::Event::Closed:
            window.close();
            break;
         case sf::Event::KeyPressed:
            if(event.key.code == sf::Keyboard::Escape)
               window.close();
            else if(event.key.code == sf::Keyboard::Return)
            {
               sf::Packet packet;
               packet<<id + ": " + text;
               socket->send(packet);
               sf::Text displayText( text, font, 20) ;
               displayText.setColor(sf::Color::Green);
               chat.push_back(displayText);
               text = "";
            }
            break;
         case sf::Event::TextEntered:
               text += event.text.unicode;
         }
      }

      sf::Packet packet1;
      socket->receive(packet1);
      std::string tempText;

      if( packet1 >> tempText )
      {
         sf::Text displayText( tempText, font, 20) ;
         displayText.setColor(sf::Color::Blue);
         chat.push_back(displayText);
      }

      int i = 0;
      for( i; i < chat.size(); i++ )
      {
         chat.setPosition( 0, i*20.0 );
         window.draw(chat);
      }

      sf::Text drawText( text, font, 20) ;
      drawText.setColor(sf::Color::White);
      drawText.setPosition( 0, i*20.0 );
      window.draw(drawText);

      window.display();
      window.clear();
   }
}

bool isAnyRequest( sf::TcpSocket * socket )
{
   socket->setBlocking(false);
   sf::Packet packet;
   string request;
   if( socket->receive(packet) )
   {
      packet>>request;
      if( request == "-10-1*0-01-r" )
      {
         sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
         window.close();
         return 1;
      }
   }
   return 0;
}

void chatOnRequest( sf::TcpSocket * socket )
{
   socket->setBlocking(true);
   sf::RenderWindow window(sf::VideoMode( 300, 400, 32), "chat");
   sf::Packet packet1;
   window.setTitle(id);
   std::string text = "";
   std::vector<sf::Text> chat;
   sf::Font font;
   if( !font.loadFromFile("Candarab.ttf") )
      cout<<"can't find the file"<<endl;
   while( window.isOpen() )
   {
      sf::Event event;
      while( window.pollEvent( event ))
      {
         switch( event.type )
         {
         case sf::Event::Closed:
            window.close();
            break;
         case sf::Event::KeyPressed:
            if(event.key.code == sf::Keyboard::Escape)
               window.close();
            else if(event.key.code == sf::Keyboard::Return)
            {
               sf::Packet packet;
               if( text == "y" && socket->isBlocking() )
               {
                  packet<<text;
                  socket->send(packet);
                  socket->setBlocking(false);
                  sf::Text displayText( text, font, 20) ;
                  displayText.setColor(sf::Color::Green);
                  chat.push_back(displayText);
                  text = "";
                  socket->setBlocking(false);
                  packet.clear();
                  window.clear();
               }
               else
               {
                  packet<<id + ": " + text;
                  sf::Text displayText( text, font, 20) ;
                  displayText.setColor(sf::Color::Green);
                  chat.push_back(displayText);
                  text = "";
               }
            }
            break;
         case sf::Event::TextEntered:
               text += event.text.unicode;
         }
      }
      sf::Packet packet1;
      socket->receive(packet1);
      std::string tempText;

      if( packet1 >> tempText )
      {
         sf::Text displayText( tempText, font, 20) ;
         displayText.setColor(sf::Color::Blue);
         chat.push_back(displayText);
      }

      int i = 0;
      for( i; i < chat.size(); i++ )
      {
         chat.setPosition( 0, i*20.0 );
         window.draw(chat);
      }

      sf::Text drawText( text, font, 20) ;
      drawText.setColor(sf::Color::White);
      drawText.setPosition( 0, i*20.0 );
      window.draw(drawText);

      window.display();
      window.clear();
   }
   
}

void checkRequest( sf::TcpSocket* socket )
{
   sf::Mutex mutex;
   sf::Thread thread( &chat, socket );
   while( true )
   {
      if( isAnyRequest( socket ) )
      {
         mutex.lock();
         thread.launch();
         mutex.unlock();
      }
   }
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: problem with multiple threads in sfml
« Reply #3 on: October 09, 2013, 02:31:11 pm »
ok this is the whole code a little bit longer........... :-[
No one will read and understand your whole code, let alone debug it. Please read this post in order to get meaningful answers. Don't forget to use [code=cpp] [/code] tags next time.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything