I'm implementing async data exchange of background thread with main thread in nonblocking mode. Found out in Unix especially iOS can use for that sockets only (based on "Advanced Programming in the UNIX Environment" W. Richard Stevens Stephen A. Rago).
So in my iOS application main thread is UI and background thread (from std) generates some data. I've implemented all needed callback from boost::asio
(async_accept
, async_read
etc.).
I've tried adding io_service.poll()
in that thread, but it looks like all callbacks are being invoked from background thread.
And I think I need to integrate io_service.run()
in NSRunLoop
. How can I do that?
Thanks.
Asked
Active
Viewed 447 times
3
-
With things like this you're trying to mix event handlers (asio and NSRunLoop). Unless they're both designed for this it cannot be made to work. You'll need to find something they both understand to inter-operate. That might have to be some shared memory, semaphores, and timed events on both sides to periodically access it. – bazza Jul 09 '17 at 18:40
-
Yes, I've read the doc. But found out, that not a problem to integrate to Qt app. So why it's a problem for others UI frameworks? But anyway, I've added `io_service.poll()` via public function to runloop observer. – Dmitry Kr Jul 10 '17 at 07:44
-
Did that work? I guess that some UI framework designers thought about I/O (like Qt - it knows about select() on *nix and WaitForMultipleObjects() on Windows, etc), whilst others didn't. Qt mixed with ZeroMQ looks particularly easy and powerful (`zmq_socket.getsockopt(ZMQ_FD, &fd, &fd_len);` gives you a file descriptor that Qt can select() on). For Boost you can get at the underlying sockets - see https://stackoverflow.com/a/12884031/2147218 - which would allow Qt (or maybe NSRunLoop if it can handle IO as well as UI events) to wake up when there's something to do on a socket. – bazza Jul 10 '17 at 19:47
-
There's this answer too: https://stackoverflow.com/a/12092176/2147218. So instead of using an io_service to handle the asio sockets, let NSRunLoop spot when a socket is ready having set up handlers there. Or, something like that. – bazza Jul 10 '17 at 19:58
-
One of the reasons I like ZMQ is that it reads a whole message, and then tells you it has arrived. In that case you can then service the socket with no further IO delay. – bazza Jul 10 '17 at 19:59
-
This might be useful: https://developer.apple.com/documentation/foundation/nssocketport/1399484-initwithprotocolfamily?language=objc – bazza Jul 10 '17 at 20:06
-
Regarding to run loop it doesn't work as expected. It has some stuck. – Dmitry Kr Jul 11 '17 at 01:01
-
io_service running in different thread is good solution and should work as expected. once message received, the next should be triggered to be requested. this is good example, session class shows how it works: http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp in this case read triggers write and write triggers read. – Sergei Krivonos Oct 26 '17 at 11:53