Created
November 13, 2013 08:50
-
-
Save jowl/7445809 to your computer and use it in GitHub Desktop.
This gist contains an example showing that LINGER cannot be set after ETERM has been caught (see https://github.com/imatix/zguide/issues/370).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <zmq.h> | |
#include <pthread.h> | |
#include <assert.h> | |
static void * worker_routine (void *context) { | |
char buf; | |
void *socket = zmq_socket (context, ZMQ_PAIR); | |
assert( zmq_connect (socket, "inproc://linger") > -1 ); | |
assert( zmq_send(socket, &buf, 1, 0) > -1); // unblock main thread | |
// zmq_recv will block until context is terminated | |
assert( zmq_recv(socket, &buf, 1, 0) == -1 ); | |
assert( errno == ETERM ); | |
printf("Caught ETERM, will attempt to set LINGER to 1000\n"); | |
int linger = 1000; | |
if( zmq_setsockopt(socket, ZMQ_LINGER, &linger, sizeof linger) == -1 ) { | |
printf("Couldn't set LINGER: %s\n", zmq_strerror(errno)); | |
} | |
assert( zmq_close (socket) > -1 ); | |
return NULL; | |
} | |
int main (void) | |
{ | |
void *context = zmq_ctx_new (); | |
pthread_t worker; | |
void *socket = zmq_socket(context, ZMQ_PAIR); | |
zmq_bind(socket, "inproc://linger"); | |
pthread_create (&worker, NULL, worker_routine, context); | |
char buf = 'x'; | |
assert( zmq_recv(socket, &buf, 1, 0) > -1 ); // wait for worker thread | |
assert( zmq_close(socket) > -1); | |
assert( zmq_ctx_destroy (context) > -1 ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment