Skip to content

Instantly share code, notes, and snippets.

@claws
Last active December 22, 2015 01:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claws/6394946 to your computer and use it in GitHub Desktop.
Save claws/6394946 to your computer and use it in GitHub Desktop.
These simple code examples were used as a code reference for a question to the zeromq mailing list about "Context was terminated" printed to stdout. The -fixed versions appear to resolve the issue.
// This simple code example was used as a code reference for a
// question to the zeromq mailing list.
//
// gcc -o threads-example.o -c threads-example.c -Wall -Wimplicit -Wextra -I/usr/local/include
// gcc -o threads-example threads-example.o -L/usr/local/lib -lzmq -lczmq
//
#include "czmq.h"
// subdue warnings
#define UNUSED(x) (void)x
static void attached_thread_task (void *args, zctx_t *ctx, void *pipe)
{
UNUSED(args);
UNUSED(ctx);
// This thread receives ping and sends pongs over the inproc
// pipe back to the main thread.
while (!zctx_interrupted)
{
char *ping = zstr_recv (pipe);
if (ping)
{
printf("T:received: %s\n", ping);
if (ping[0] == '\0')
{
// zero length message instructs thread to shutdown
free (ping);
break;
}
free (ping);
printf("T:sending: pong\n");
zstr_send (pipe, "pong");
}
}
printf("\nfinishing thread.\n");
}
int main (void) {
zctx_t *ctx = zctx_new();
// spin off a thread to perform the pong'ing. This thread returns
// an inproc socket.
void *ping = zthread_fork(ctx, attached_thread_task, NULL);
assert(ping);
while (!zctx_interrupted)
{
// send ping
printf("M:sending: ping\n");
zstr_send (ping, "ping");
char *pong = zstr_recv (ping);
if (pong == NULL)
{
//zsocket_disconnect(ping, endpoint);
break; // interupted
}
printf("M:received: %s\n", pong);
free (pong);
// Comment out this sleep to remove the "Context was terminated"
// error reported to stdout.
zclock_sleep(1000);
}
// instruct thread shutdown and give it some grace time
zstr_send(ping, "");
zclock_sleep(500);
zctx_destroy(&ctx);
return 0;
}
// This simple code example was used as a code reference for a
// question to the zeromq mailing list.
//
// gcc -o threads-example-loop.o -c threads-example-loop.c -Wall -Wimplicit -Wextra -I/usr/local/include
// gcc -o threads-example-loop threads-example-loop.o -L/usr/local/lib -lzmq -lczmq
//
#include "czmq.h"
// subdue warnings
#define UNUSED(x) (void)x
typedef struct {
zctx_t *ctx;
zloop_t *loop;
void *pipe;
} app_t;
static void attached_thread_task (void *args, zctx_t *ctx, void *pipe)
{
UNUSED(args);
UNUSED(ctx);
while (!zctx_interrupted)
{
char *ping = zstr_recv (pipe);
if (ping)
{
printf("T:received: %s\n", ping);
// zero length message instructs thread to shutdown
if (ping[0] == '\0')
{
free (ping);
break;
}
free (ping);
printf("T:sending: pong\n");
zstr_send (pipe, "pong");
}
}
printf("\nfinishing thread.\n");
}
static int handle_ping_timer_expiry (zloop_t *loop, zmq_pollitem_t *poller, void *args)
{
UNUSED(loop);
UNUSED(poller);
app_t *self = (app_t*) args;
printf("M:sending: ping\n");
zstr_send (self->pipe, "ping");
return 0;
}
static int handle_pong (zloop_t *loop, zmq_pollitem_t *poller, void *args)
{
UNUSED(loop);
UNUSED(poller);
app_t *self = (app_t*) args;
char *pong = zstr_recv (self->pipe);
if (!pong)
{
printf("interrupted...\n");
zloop_timer_end (self->loop, self);
return -1; // interupted, stop timer
}
printf("M:received: %s\n", pong);
free (pong);
return 0;
}
int main (void) {
app_t *self = (app_t *) zmalloc (sizeof (app_t));
self->ctx = zctx_new();
// spin off a thread to perform pong
self->pipe = zthread_fork(self->ctx, attached_thread_task, NULL);
assert(self->pipe);
self->loop = zloop_new();
zloop_set_verbose (self->loop, 1);
zmq_pollitem_t poller = { self->pipe, 0, ZMQ_POLLIN, 0 };
zloop_poller(self->loop, &poller, handle_pong, self);
zloop_timer(self->loop, 1000, 0, handle_ping_timer_expiry, self);
zloop_start(self->loop);
zloop_destroy(&self->loop);
// instruct thread shutdown and give it some grace time
zstr_send(self->pipe, "");
zclock_sleep(500);
zctx_destroy(&self->ctx);
return 0;
}
// This simple code example was used as a code reference for a
// question to the zeromq mailing list.
//
// gcc -o threads-example-loop.o -c threads-example-loop.c -Wall -Wimplicit -I/usr/local/include
// gcc -o threads-example-loop threads-example-loop.o -L/usr/local/lib -lzmq -lczmq
//
#include "czmq.h"
static void attached_thread_task (void *args, zctx_t *ctx, void *pipe)
{
while (!zctx_interrupted)
{
char *ping = zstr_recv (pipe);
if (ping == NULL)
{
break; // interupted
}
printf("T:received: %s\n", ping);
free (ping);
printf("T:sending: pong\n");
zstr_send (pipe, "pong");
}
printf("thread task finishing.\n");
}
static int handle_ping_timer_expiry(zloop_t *loop, zmq_pollitem_t *poller, void *args)
{
void *monitor = args;
printf("M:sending: ping\n");
zstr_send (monitor, "ping");
return 0;
}
static int handle_pong(zloop_t *loop, zmq_pollitem_t *poller, void *args)
{
char *pong = zstr_recv (poller->socket);
if (pong == NULL)
{
return -1; // interupted, stop timer
}
printf("M:received: %s\n", pong);
free (pong);
return 0;
}
int main (void) {
zctx_t *ctx = zctx_new();
// spin off a thread to connect to perform pong
void *monitor = zthread_fork(ctx, attached_thread_task, NULL);
assert(monitor);
zloop_t *loop = zloop_new();
zmq_pollitem_t poller = { monitor, 0, ZMQ_POLLIN, 0 };
zloop_poller(loop, &poller, handle_pong, NULL);
zloop_timer(loop, 1000, 0, handle_ping_timer_expiry, monitor);
zloop_start(loop);
zloop_destroy(&loop);
zsocket_destroy(ctx, monitor);
zctx_destroy(&ctx);
return 0;
}
// This simple code example was used as a code reference for a
// question to the zeromq mailing list.
//
// gcc -o threads-example.o -c threads-example.c -Wall -Wimplicit -I/usr/local/include
// gcc -o threads-example threads-example.o -L/usr/local/lib -lzmq -lczmq
//
#include "czmq.h"
static void attached_thread_task (void *args, zctx_t *ctx, void *pipe)
{
// This thread receives ping and sends pongs over the inproc
// pipe back to the main thread.
while (!zctx_interrupted)
{
char *ping = zstr_recv (pipe);
if (ping == NULL)
{
break; // interupted
}
printf("T:received: %s\n", ping);
free (ping);
printf("T:sending: pong\n");
zstr_send (pipe, "pong");
}
printf("\nfinishing thread.\n");
}
int main (void) {
zctx_t *ctx = zctx_new();
// spin off a thread to perform the pong'ing. This thread returns
// an inproc socket.
void *ping = zthread_fork(ctx, attached_thread_task, NULL);
assert(ping);
while (!zctx_interrupted)
{
// send ping
printf("M:sending: ping\n");
zstr_send (ping, "ping");
char *pong = zstr_recv (ping);
if (pong == NULL)
{
//zsocket_disconnect(ping, endpoint);
break; // interupted
}
printf("M:received: %s\n", pong);
free (pong);
// Comment out this sleep to remove the "Context was terminated"
// error reported to stdout.
zclock_sleep(1000);
}
zctx_destroy(&ctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment