I am using jzmq
package for my project to communicate over network. I am using DEALER ROUTER
pair. I have read that the socket with DEALER
and ROUTER
type is not thread safe. So I can not send or receive from the same socket on 2 different threads.
My questions are:
1 ) What is the purpose of the ZThread
class in the jzmq package?
2 ) Does it handle this thread unsafety?
3 ) Can I send and receive from the same socket if using it from a parent thread and its child ZThread
?
4 ) Also what is the difference between Attached
and AdetachedRunnable
?
In spite of the most recent efforts ( published in late 2017 on 4.2+ total re-design efforts towards removing this known initial principle ), ZeroMQ education materials present wherever possible and also explain why there is a bad habit to try to share toys in distributed-system design practice.
Even if one gets some API-baked promise, always first benchmark the performance, if one will ever want to pay the costs of lost performance for such ex-post sharing doctrine. As noted about the ZeroMQ native API, there is principally nothing to be shared ( with one exception, which sometimes may make sense, the global Context()-instance ). Threads may "borrow" the IO-socket instantiations from such a global Context()-instance, but never share socket-instances, as the results are not guaranteed inside and under the ZeroMQ native API and so will not be any better even if promised so "above" any kind of higher level API.
as per 2 ), never share sockets, there is no reason to try to do that. If managing resources ( and threads are first-class citizen among resources ), better create a private, point to point PAIR/PAIR
or PUSH/PULL
( even in tandem of simplex pipes ) over { inproc:// | ipc:// }
-transport-class ( where inproc://
can for performance motivated cases even use another "co-locally-isolated" private Context(0)
-instance, having indeed zero IO-threads at all ) and enjoy the due separation of concerns with minimum adverse effects on principally lost thread-safety ( if not doing so ) and performance.
( CZMQ/3.0.1 API docs )
zthread
- working with system threads (deprecated)
...
Thezthread
class wraps OS thread creation. It creates detached threads that look like normal OS threads, or attached threads that share the caller's ØMQ context, and get aninproc
pipe to talk back to the parent thread. Detached threads create their own ØMQ contexts as needed. NOTE: this class is deprecated in favor ofzactor
.
One may better check the version creeps among native API used, the binding / wrapper version and documentation.
Yet, Zero sharing Zen may lead your steps ( if selected language binding permits one to remain free in design decisions - reading the original design motivations always helps understand the performance and safety insights from The Original ).