zctx(3)

zctx(3)

CZMQ Manual - CZMQ/1.4.1

Name

zctx - working with ØMQ contexts

Synopsis

//  Create new context, returns context object, replaces zmq_init
CZMQ_EXPORT zctx_t *
    zctx_new (void);

//  Destroy context and all sockets in it, replaces zmq_term
CZMQ_EXPORT void
    zctx_destroy (zctx_t **self_p);

//  Raise default I/O threads from 1, for crazy heavy applications
CZMQ_EXPORT void
    zctx_set_iothreads (zctx_t *self, int iothreads);

//  Set msecs to flush sockets when closing them
CZMQ_EXPORT void
    zctx_set_linger (zctx_t *self, int linger);

//  Set HWM value. This is used in zthread_fork
CZMQ_EXPORT void
    zctx_set_hwm (zctx_t *self, int hwm);

//  Get HWM value. This is used in zthread_fork
int
    zctx_hwm (zctx_t *self);

//  Return low-level ØMQ context object, will be NULL before first socket
//  is created. Use with care.
CZMQ_EXPORT void *
    zctx_underlying (zctx_t *self);

//  Self test of this class
CZMQ_EXPORT int
    zctx_test (bool verbose);

//  Global signal indicator, TRUE when user presses Ctrl-C or the process
//  gets a SIGTERM signal.
CZMQ_EXPORT extern volatile int zctx_interrupted;

Description

The zctx class wraps ØMQ contexts. It manages open sockets in the context and automatically closes these before terminating the context. It provides a simple way to set the linger timeout on sockets, and configure contexts for number of I/O threads. Sets-up signal (interrupt) handling for the process.

The zctx class has these main features:

  • Tracks all open sockets and automatically closes them before calling zmq_term(). This avoids an infinite wait on open sockets.
  • Automatically configures sockets with a ZMQ_LINGER timeout you can define, and which defaults to zero. The default behavior of zctx is therefore like ØMQ/2.0, immediate termination with loss of any pending messages. You can set any linger timeout you like by calling the zctx_set_linger() method.
  • Moves the iothreads configuration to a separate method, so that default usage is 1 I/O thread. Lets you configure this value.
  • Sets up signal (SIGINT and SIGTERM) handling so that blocking calls such as zmq_recv() and zmq_poll() will return when the user presses Ctrl-C.

Example

From zctx_test method

 // Create and destroy a context without using it
 zctx_t *ctx = zctx_new ();
 assert (ctx);
 zctx_destroy (&ctx);
 assert (ctx == NULL);

 // Create a context with many busy sockets, destroy it
 ctx = zctx_new ();
 assert (ctx);
 zctx_set_iothreads (ctx, 1);
 zctx_set_linger (ctx, 5); // 5 msecs
 void *s1 = zctx__socket_new (ctx, ZMQ_PAIR);
 void *s2 = zctx__socket_new (ctx, ZMQ_XREQ);
 void *s3 = zctx__socket_new (ctx, ZMQ_REQ);
 void *s4 = zctx__socket_new (ctx, ZMQ_REP);
 void *s5 = zctx__socket_new (ctx, ZMQ_PUB);
 void *s6 = zctx__socket_new (ctx, ZMQ_SUB);
 zsocket_connect (s1, "tcp://127.0.0.1:5555");
 zsocket_connect (s2, "tcp://127.0.0.1:5555");
 zsocket_connect (s3, "tcp://127.0.0.1:5555");
 zsocket_connect (s4, "tcp://127.0.0.1:5555");
 zsocket_connect (s5, "tcp://127.0.0.1:5555");
 zsocket_connect (s6, "tcp://127.0.0.1:5555");
 assert (zctx_underlying (ctx));  zctx_destroy (&ctx);

See also

czmq(7)

Authors

The CZMQ manual was written by Pieter Hintjens<moc.xitami|hp#moc.xitami|hp>.

Resources

Main web site: http://czmq.zeromq.org/

Report bugs to the ØMQ development mailing list: <gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez>

Copyright

Copyright (c) 1991-2010 iMatix Corporation and contributors. License LGPLv3+: GNU LGPL 3 or later <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to change it and redistribute it. There is NO WARRANTY, to the extent permitted by law. For details see the files COPYING and COPYING.LESSER included with the CZMQ distribution.