1

I'm developing an instant messaging application.

This is the situation which I need help: A routine in my code fgets() the message the user has entered. Now I need to wake up a thread which has a routine to send the message to the socket etc. I'm not really sure how to do this.

If I'm using a mutex: I dont want my first thread to ever wait. Hence i dont want to use this. Similarly I cant use cond_variable.

Please tell me how to get this.

Duck
  • 26,924
  • 5
  • 64
  • 92
Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
  • 2
    Don't over think it. The amount of time your two threads will spend (a) waiting for user input and (b) transmitting over the network are EONS compared to what you imagine a mutex/condvar will cost. – Duck Mar 28 '12 at 22:53
  • 1
    Why can't you use a condition variable? Condition variables are *designed* to wake up a thread whenever some event occurs, so they could come in quite handy here. – Adam Mihalcin Mar 28 '12 at 23:05

1 Answers1

0

Duck's point about not overthinking it is a good one.

Another way you could go is to use a pipe. Your console handling thread writes a message to the pipe, and the network thread does a blocking read from the pipe.

What you might end up with is the network thread doing a select() on both the console pipe and the network socket. Then it would wake up and do things when it either had something to send, or something to receive from the network. Snazzy!

blueshift
  • 6,742
  • 2
  • 39
  • 63