Original Post
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: I would like some documentation on how to make a thread the easiest way. thnx in advance -Joe
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
}