Skip to content

Instantly share code, notes, and snippets.

@StefanKarpinski
Created April 8, 2014 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanKarpinski/10083996 to your computer and use it in GitHub Desktop.
Save StefanKarpinski/10083996 to your computer and use it in GitHub Desktop.
thread communication with ZMQ
// gcc -shared -fPIC -I$JULIAHOME/usr/include -L$JULIAHOME/usr/lib -lzmq -luv -o libthreadzmq.so threadzmq.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>
#include <zmq.h>
#include <uv.h>
static void free_buffer(void *data, void *hint) { free(data); }
void produce_messages(void *sock)
{
int i;
for (i = 0;; i++) {
char *buf;
zmq_msg_t msg;
int len = asprintf(&buf, "[message #%d from %p]", i, sock);
assert(zmq_msg_init_data(&msg, buf, len, free_buffer, NULL) == 0);
assert(zmq_sendmsg(sock, &msg, 0) != -1);
usleep(lroundl(1e6*drand48()));
}
return;
}
void start_pusher(void *sock) {
uv_thread_t thread;
uv_thread_create(&thread, produce_messages, sock);
return;
}
using ZMQ
ctx = Context(1)
pull = Socket(ctx, PULL)
pushers = [ Socket(ctx, PUSH) for _ = 1:10 ]
ZMQ.bind(pull, "inproc://queue")
for pusher in pushers
ZMQ.connect(pusher, "inproc://queue")
ccall((:start_pusher, :libthreadzmq), Void, (Ptr{Void},), pusher.data)
end
while true
println(bytestring(ZMQ.recv(pull)))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment