Re: using select() without blocking the runloop


On Nov 24, 2017, at 12:57 AM, Gerriet M. Denkmann <g@...> wrote:

I need to do select() with a rather large timeout value (several minutes).
So in order not to block the runLoop / mainThread I tried:

Don’t use dispatch queues for long blocking operations. It messes up libDispatch’s scheduling. For a task like this, you should explicitly create a thread using either NSThread or pthreads.

(Also, there’s almost never a need to use select() in Cocoa code. Is there a reason you can’t use any higher level network code like CFStream, NSStream, or NSURLSession?)

dispatch_queue_t myQueue = dispatch_get_global_queue( QOS_CLASS_BACKGROUND, 0 );
dispatch_async( myQueue, ^void(void)
selectResult = select(one_highest_fd, p_read_fds, p_write_fds, p_except_fds, ptv);

The likely problem here is that several of these parameters are pointers, and the buffers they point to may no longer be valid by the future time that the block runs … especially if they’re local variables of the enclosing function/method/


Join to automatically receive all group messages.