Skip to content

Instantly share code, notes, and snippets.

@jowl
Created November 13, 2013 08:50
Show Gist options
  • Save jowl/7445809 to your computer and use it in GitHub Desktop.
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).
#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