mtserver | Example code from my Multithreaded Work Queue Based Server | Application Framework library
kandi X-RAY | mtserver Summary
kandi X-RAY | mtserver Summary
Example code from my Multithreaded Work Queue Based Server in C++ blog.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of mtserver
mtserver Key Features
mtserver Examples and Code Snippets
Community Discussions
Trending Discussions on mtserver
QUESTION
I'm new to zmq and cppzmq. While trying to run the multithreaded example in the official guide: http://zguide.zeromq.org/cpp:mtserver
My setup
- macOS Mojave, Xcode 10.3
- libzmq 4.3.2 via Homebrew
- cppzmq GitHub HEAD
I hit a few problems.
Problem 1
When running source code in the guide, it hangs forever without any stdout output shown up.
Here is the code directly copied from the Guide.
...ANSWER
Answered 2019-Sep-14 at 08:48Suspect #1: the code jumps straight into an unresolveable live-lock due to a move into ill-directed state of the distributed-Finite-State-Automaton:
While I since ever advocate for preferring non-blocking .recv()
-s, the code above simply commits suicide right by using this step:
socket.recv( request, zmq::recv_flags::dontwait ); // socket being == ZMQ_REP
kills all chances for any other future life but the very error The zmq_send() operation cannot be performed on this socket at the moment due to the socket not being in the appropriate state.
as
going into the .send()
-able state is possible if and only if a previous .recv()
-ed has delivered a real message.
Review the code and may either use a blocking-form of the .recv()
before going to .send()
or, better, use a { blocking | non-blocking }-form of .poll( { 0 | timeout }, ZMQ_POLLIN )
before entering into an attempt to .recv()
and keep doing other things, if there is nothing to receive yet ( so as to avoid the self suicidal throwing the dFSA into an uresolvable collision, flooding your stdout/stderr
with a second-spaced flow of printf(" ERROR: %X\n", e.num() );
)
Better use const char *zmq_strerror ( int errnum );
being fed by int zmq_errno (void);
On the contrary to the suicidal ::dontwait
flag in the Problem 2 root cause, the Problem 2 root cause is, that a blocking-form of the first .recv()
here moves all the worker-threads into an undeterministically long, possibly infinite, waiting-state, as the .recv()
-blocks proceeding to any further step until a real message arrives ( which it does not seem from the MCVE, that it ever will ) and so your pool-of-threads remains in a pool-wide blocked-waiting-state and nothing will ever happen until any message arrived.
REQ/REP
works :
The REQ/REP
Scalable Communication Pattern Archetype works like a distributed pair of people - one, let's call her Mary, asks ( Mary .send()
-s the REQ
), while the other one, say Bob the REP
listens in a potentially infinitely long blocking .recv()
( or takes a due care, using .poll()
to orderly and regularly check, if Mary has asked about something or not and continues to do his own hobbies or gardening otherwise ) and once the Bob's end gets a message, Bob can go and .send()
Mary a reply ( not before, as he knows nothing when and what Mary would ( or would not ) ask in the nearer of farther future ) ) and Mary is fair not to ask her next REQ.send()
-question to Bob anytime sooner but after Bob has ( REP.send()
) replied and Mary has received Bob's message ( REQ.recv()
) - which is fair and more symmetric, than a real life may exhibit among real people under one roof :o)
The code?
The code is not a reproducible MCVE. The main()
creates five Bobs ( hanging waiting a call from Mary, somewhere over inproc://
transport-class ), but no Mary ever calls, or does she? Not visible sign of any Mary trying to do so, the less her ( their, could be a (even a dynamic) community of N:M herd-of-Mary(s):herd-of-5-Bobs relation ) attempt(s) to handle REP-ly(s) coming from either one of the 5-Bobs.
Persevere, ZeroMQ took me some time of scratching my own head, yet the years after I took a due care to learn the Zen-of-Zero are still a rewarding eternal walk in the Gardens of Paradise. No localhost serial-code IDE will ever be able to "debug" a distributed-system (unless a distributed-inspector infrastructure is inplace, a due architecture for a distributed-system monitor/tracer/debugger is another layer of distributed messaging/signaling layer atop of the debugged distributed messaging/signaling system - so do not expect it from a trivial localhost serial-code IDE.
If still in doubts, isolate potential troublemakers - replace inproc://
with tcp://
and if toys do not work with tcp://
(where one can wire-line trace the messages) it won't with inproc://
memory-zone tricks.
QUESTION
I am building a small program (here), in which the main()
sends the same msg
to all worker threads and the worker threads simply print the msg
.
I think I have exactly followed this tutorial, from the official guide.
I pass the 0MQ context
to thread via a void pointer and then cast it back to zmq::context_t *
. But still I got the Segmentation fault. Below is info from coredump in GDB:
ANSWER
Answered 2017-May-25 at 08:25pool.push_back(thread(task1, (void *)context));
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mtserver
Get the work queue code.
Get the TCP sockets code.
Place the mtserver, threads, wqueue and tcpsockets directories at the same directory level, e.g. ${HOME}/src/mtserver, ${HOME}/src/threads, ${HOME}/src/wqueue and ${HOME}/src/tcpsockets.
cd to ${HOME}/src/mtserver.
Build the test client and server applications by running make.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page