threads in a loop being started more than once issue

I'm building a simple as chat client, it uses the console, im only really familiarizing myself with everything and so i guess the thing im least familiar with handling is threads!!

basically I have one thread for sending another for receiving in a while loop, my aim was to have the chat running smoothly without having to wait for the other person to reply before sending something else.

of course starting a thread in a loop is hopeless because it will get started and started over again, I tried using an if server isnt running then start server command but got error messages of kinds, what is the correct way to do this? tk kindly :)
Not sure what OS or threading model you're using.

I'd be inclined to create four threads at the begining of the program.
- A send thread
- A receive thread
- A keyboard thread
- A screen thread
main program then goes to sleep waiting for a termination signal.

When keyboard thread detects input, queue it to both the send thread and the screen thread. Continue waiting for input from the keyboard.
When receive thread gets a message, queue it to the screen thread and resume the receive loop.
When screen thread receives a message, update the screen and go back to waiting for a message.
When the send thread receives a message, send it out the socket. Wait for another message to be received.
That seems like a lot of threads to maintain. I was thinking more like 2 threads per client, the main thread and a receive thread.
@naraku - It keeps things very simple. Each thread had exactly one purpose.
I think i will have to play with threads and be all familiar with them, can you recomend a good exercise??
The recieve thread, would you have it in a loop? how do you get it to behave??
closed account (zb0S216C)
When you say "another for receiving in a while loop", what do you mean exactly?

@devonrevenge: You seem to be misunderstanding how threads work. What you need is not a loop, but a lock. A lock, or mutex, is a synchronisation technique used by threads to wait for a common resource to become available. While a thread is waiting for a mutex to become signalled (the state which indicates that the mutex has been unlocked by the previous owning thread), the thread will spin. Spinning, or spin-lock, is a term used to describe a thread that's waiting for a lock to become available.

When the lock becomes available one of the waiting threads will attain the lock which will grant them access to the resource which the lock was guarding.

With that being said, what you need in your code is locks. We need to see some code so we know the best place to use the locks.

Wazzak
Last edited on
devonrevenge wrote:

The recieve thread, would you have it in a loop?

Yes, the receive thread would loop.
1
2
3
4
5
6
7
8
9
10
11
12
    while (! terminate_flag)
   {  do 
      { post socket_receive operation 
         wait for socket_receive completion
         // relinquishes control until the operation completes
         // receive operation may complete with less than a full message
         // or with multiple messages
       } while (! message_complete);
       enqueue message(s) to screen thread.
       // return to the top of the loop to post another receive operation
   }
   //  exit the thread 
closed account (zb0S216C)
Don't use loops to wait for a common resource to be signalled; you get race conditions that way. Use locks instead.

Wazzak
looking up locks, i think my issue may actualy be on the server now
Last edited on
[shame]the code was in java -_-[/shame]
Topic archived. No new replies allowed.