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


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.

  • did u try using lock() and later unlock() after some fixed time so that other thread gets control of the lock? Mar 22, 2013 at 12:17
  • But these are used incase we use shared memory Mar 22, 2013 at 12:21
  • sorry I didn't see your tags ..I was thinking in Java..you must be using pthreads...no idea! Mar 22, 2013 at 12:26
  • Homework? Or why are you writing a server from scratch without background? In either case, more details required to provide useful answers; e.g. how does control get "stuck"?
    – sdg
    Mar 22, 2013 at 12:42
  • Would you provide more details surrounding your problem? I'm assuming this is one application, not two, right? Are you using semaphores / mutexes / any other signaling mechanism? What does the general structure of your send and receive loops look like?
    – user53019
    Mar 22, 2013 at 12:43

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.