Multi-threaded server

cmultithreading

I have written a server/client program in which I am using 2 threads :

  • One to recieve data continously and
  • Other to send data as the user writes it on screen

Problem:

I have created the threads but durung execution ,the control gets stuck in
the recieving thread having the infinite loop to recieve data and never goes
to the sending thread.

Please tell me if there is any solution to the problem.

Best Answer

Have a look at this tutorial for pthreads. Even if you're not using pthreads, the general concepts will still apply to the threading library you're using.

In particular, look at Section 7 for mutexes and Section 8 for conditional variables. At first glance, it doesn't appear that you're using any sort of signaling or mutexes in order to control the flow of your program. One thread dominating the operations is a tell-tale sign of that problem.

Assuming you're using a single area to pass data between your two threads, you'll need some sort of mechanism to control access over that data area. A mutex is the general route to solve that challenge, but you can use semaphores to control the access as well.


As an alternative and if you're running on a Linux / POSIX system that supports FIFO pipes, you could use that instead. You'll need to weigh the cost of system access to the FIFO versus the implementation cost of mutexes. Personally, I think FIFOs are a replacement for IPC (inter-process communication), and are not all that great of an option instead of a mutex. But it's an alternative and if you can't wrap your head around using a mutex or semaphore, then FIFOs are an option.

Related Topic