views:

84

answers:

1

I have the following scenario:
The program has a reader and a writer thread to a socket which is an external application. The reader and writer need to work in coordinated cycles.
The writer sends a dynamic number of messages (x) to the socket and the reader needs to read the responses from the socket. The number of messages is over 5 to 10k per cycle. The messages are not received in the same order as sent and the message have a clear layout, so it is possible to determine single messages.
The reader needs to read x messages and perform then some processing after the . The writer needs to restart sending to the socket after the reader has performed the after-reading processing.
What is the fastest synchronisation from your pov?

  • Sending a special message to the socket with the number of written messages? (Even though there is no FIFO guarantee on the socket)
  • Work with the classical locking object?
  • Use atomic-transactions for integers with the number of written messages and let the writer update the integer only once?

    Have I missed any other fast synchronizing mechanism?

+3  A: 

Unless you are doing this on the order of 100,000 times per second, it doesn't really matter which mechanism is microseconds faster than another. Just use whatever mechanism is the easiest to understand and/or get working. Then only consider optimizing the mechanism if it turns out to not be fast enough. More than likely the message processing itself will use several orders of magnitude more time than this synchronization.

Gabe
Exactly! I was going to say the same thing :)
Sergius
I was bad on formulating my question- synching needs to be checked after each read. Thus several thousand times per second, the actual cycles/state changes happen only every 1-2 seconds.
weismat