How to run CFSocket on a background thread for more accurate ping time?
Background...
I am modifying Apple’s SimplePing example to do an ICMP ping for an iPhone app. The code wraps a native socket in a CFSocket object specifying a read callback, then adds it as a run loop source on the main thread. When a packet is sent to the socket, the callback is invoked开发者_高级运维 to time the round trip, verify the contents, update the UI, etc.
Question...
What would be the best approach for moving this processing to a background thread so the ping time is as accurate as possible? I need to measure the precise time between the call to the socket “sendto” method and the callback invocation without interruption.
Any examples or pseudo code would be extremely helpful. I have done a lot of reading on threading in Cocoa (NSThread vs. NSOperation, NSRunLoop, etc.), but so far, I can’t quite piece it all together.
Thanks
Do you need to support iOS 3.x? If not, you could look into using Grand Central Dispatch; in this scenario, you would specify the socket as a source for a dispatch queue and give it the highest priority.
精彩评论