I am creating a multithreaded socket program that needs to send 2 sets of data to the server periodically. The client needs to send GPS information every 3 seconds and polarity information every 4 seconds. My idea on implementing this was to simply create a new thread, make a infinite loop, send() the GPS information, sleep() for 1 second, send() the polarity information, and then sleep() for 2 seconds. Would this be a sufficient way of going about this? Sorry, I'm new to socket programming so I don't know of any alternative methods.
-
1I see no reason why your program must be multithreaded. Your solution seems reasonable. – Jonathon Reinhart Dec 07 '14 at 17:07
-
That sends the polarity information every 3 seconds, not every 4 seconds. No; it doesn't meet the specification. Assuming you need to use threads at all, use two threads, one on a three second wait, one on a four second wait. Coordinate access to the socket with a mutex so that on multiples of 12 seconds, you still get sane behaviour. However, you could do it all with a single-threaded process that waits appropriate intervals. After sending both, it sleeps 3, sends GPS, sleeps 1, sends polarity, sleeps 2, sends GPS, sleeps 2, sends polarity, sleeps 1, sends GPS, sleeps 3, sends both. – Jonathan Leffler Dec 07 '14 at 17:08
-
`send` may not send all the data at once, so you need a loop around each `send`. – alain Dec 07 '14 at 17:08
-
why two `sleep`s, when one is enough? – Karoly Horvath Dec 07 '14 at 17:12
-
@alain POSIX guarantees that it will handle all data for blocking sockets (then if it actually hits the network or not is another question, however it wont make any difference for `send`). For non-blocking sockets, what you write is true. – Jite Dec 07 '14 at 17:20
1 Answers
Your proposed scheme is:
send the GPS information, sleep for 1 second, send the polarity information, and then sleep for 2 seconds
Over a period of 12 seconds, that leads to:
- GPS; sleep 1
- polarity; sleep 1
- sleep 1
- GPS; sleep 1
- polarity; sleep 1
- sleep 1
- GPS; sleep 1
- polarity; sleep 1
- sleep 1
- GPS; sleep 1
- polarity; sleep 1
- sleep 1
This sends the GPS information 4 times (correct) and the polarity 4 times, and every 3 seconds, not 3 times and every 4 seconds as the specification requires. The desired sequence might be:
- GPS; polarity; sleep 1
- sleep 1
- sleep 1
- GPS; sleep 1
- polarity; sleep 1
- sleep 1
- GPS; sleep 1
- sleep 1
- polarity; sleep 1
- GPS; sleep 1
- sleep 1
- sleep 1
Note that in some second, both GPS and polarity must be sent.
You can achieve this in either of two ways. Assuming you need to use threads at all, use two threads, one on a three second wait, one on a four second wait. Coordinate access to the socket with a mutex so that on multiples of 12 seconds, you still get sane behaviour.
However, you could do it all with a single-threaded process that waits appropriate intervals:
int i_gps = 3;
int i_pol = 4;
int t_gps = 0;
int t_pol = 0;
int t_cur = 0;
while (1)
{
if (t_cur == t_gps && t_cur == t_pol)
{
t_cur = t_gps = t_pol = 0; // Avoid integer overflow
}
if (t_cur == t_gps)
{
send GPS
t_gps += i_gps;
}
if (t_cur == t_pol)
{
send polarity
t_pol += i_pol;
}
sleep 1;
t_cur++;
}
You can also sleep for multiple seconds by altering the code at the end of the loop to determine which send comes next and sleeping for the appropriate amount of time and incrementing t_cur
by the appropriate amount of time (rather than just using 1 second all the time).
If you need to avoid drift (because the code above assumes that the send process is instantaneous and it isn't), then you have to use sub-second sleeps and trap the time at the start of the loop and adjust the sleep interval to allow for the time taken to do the sending.

- 730,956
- 141
- 904
- 1,278
-
Thank you, I understand this but the gps being 3 seconds and polarity being 4 seconds was only an example. I'm having difficulty thinking of how to implement this if the gps and polarity intervals are some other arbitrary values. – marcusc Dec 09 '14 at 14:54
-
The code I showed can be adapted to (more or less) arbitrary values for the intervals (`i_gap` and `i_pol`), subject only to the ranges of valid integers. It can also be generalized to handle an arbitrary number of message types with a little care and some function pointers. You'd need to think a bit harder if you have gaps of 4 and 8 seconds, but you wanted the 4 second operations to occur on 0, 4, 8, 12, 16, 20, … while you wanted the 8 second operations to occur at 2, 10, 18, … – Jonathan Leffler Dec 09 '14 at 15:13