Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ threading

Started by JoeCatshoe Apr 3, 2007 at 4:34 PM 1 replies 600+ views
Original Post
JoeCatshoe
JoeCatshoe
Hi, I'm developing a simple client-client application where the clients exchange text messages using winsock. Now, on the client I want to be able to check for incoming data as well be able to send data at the same time. I'm checking for incoming data with a constant while loop with recv() in it. If I want to be able to send a message to the other client, this while loop has to be stopped in order to be able to handle input and I could miss data send by the other user. I heard about threading from a friend. Is it possible to have one thread handling the receiving of the data, another sending it and the "main" thread just displaying the gathered data of the receive thread to the screen. I found this piece of code on the net but it gives me some compile errors:

class Thread
{
   public:
      Thread();
      int Start(void * arg);
   protected:
      int Run(void * arg);
      static void * EntryPoint(void*);
      virtual void Setup();
      virtual void Execute(void*);
      void * Arg() const {return Arg_;}
      void Arg(void* a){Arg_ = a;}
   private:
      THREADID ThreadId_;
      void * Arg_;

};

Thread::Thread() {}
//initialize the thread
int Thread::Start(void * arg)
{
   Arg(arg); // store user data
   int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
   return code;
}
//run the thread
int Thread::Run(void * arg)
{
   Setup();
   Execute( arg );
}

/*static */
void * Thread::EntryPoint(void * pthis)
{
   Thread * pt = (Thread*)pthis;
   pthis->Run( Arg() );
}

virtual void Thread::Setup()
{
        // Do any setup here
}

virtual void Thread::Execute(void* arg)
{
        // Your code goes here
}



I would like some documentation on how to make a thread the easiest way. thnx in advance -Joe
Antheus
Antheus
Yes, it's possible. It's even required for any kind of scalable solution.

The most basic one requires two threads, performing blocking read and write on a socket.

More complex ones use IO completion ports or other forms of non-blocking networking.

For threading, you have once again several options. Using native or portable threading libraries, along with some higher networking libraries that make threading transparent.

Unfortunately, threading also brings about synchronization issues, so that's what you need to look into, or your application will be dying in hanging in most unusual and obscure ways.

For network IO, you might want to have two queues. One for incoming data, one for outgoing. Then you have two threads, one is reading from the socket and putting data into incoming queue. Other is reading from outgoing queue and writing data to the socket. Third thread is your application thread, that reads from incoming queue, processes the data, and puts results into outgoing queue.

But as I said, there's so many issues there isn't a simple answer or a library you just pop in and get the benefit.

Some terms to search for are pthreads, boost::threads, concurrent programming, mutli-threading, mutex, locks, synchronization, windows threads, critical section, and many more....

It's a big topic.



JoeCatshoe
JoeCatshoe
Thnx for your reply.

I know that synchronizing threads isn't easy, so I hope I can manage it since I'm just a beginner :)

I'm now looking into the Pthread library, let's see how it turns out.

-Joe

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.