Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
did u try using lock() and later unlock() after some fixed time so that other thread gets control of the lock? –  Rorschach Mar 22 '13 at 12:17
    
But these are used incase we use shared memory –  user3032010 Mar 22 '13 at 12:21
    
sorry I didn't see your tags ..I was thinking in Java..you must be using pthreads...no idea! –  Rorschach Mar 22 '13 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 '13 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? –  GlenH7 Mar 22 '13 at 12:43
show 1 more comment

1 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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