multithread single socket

Hi,

I'm writing a messaging client, for a single socket. I have 2 threads: one that merely responds to server requests and one that starts sending messages at a specific time. Now, I found that on linux, this might result in a data order problem: when the 2 threads call the 'send()' function at the same time, the data might arrive split and mangled. What I mean is that in this case, the server might first receive the first 50% of packet 1, then the whole packet 2 and then the remainder of packet 1.

Now, my application is extremely performance/latency sensitive, how do I solve this with minimum performance impact ?

Thanks a lot guys !
If both applications are on the same processor, then you may want to look into IPC shared memory to transfer data. It'll be far quicker with lower latency.

http://www.cs.cf.ac.uk/Dave/C/node27.html

You can also serialize your data for both the socket or shared memory for ease of transfering the data. I recommend the boost library for this:
http://www.boost.org/doc/libs/1_41_0/libs/serialization/doc/index.html

As to the specifics of yoru question, I am unsure, but it could be that you need to decrease the packet size to stop the packet splitting.
Last edited on
Topic archived. No new replies allowed.