Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "zhelpers.h"
- #include <zmq.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- struct sample
- {
- int b;
- int c;
- char str[32];
- };
- struct example
- {
- int a;
- int b;
- struct sample *obj2;
- };
- static void *
- worker_routine (void *context) {
- // Socket to talk to dispatcher
- void *receiver = zmq_socket (context, ZMQ_PAIR);
- zmq_connect (receiver, "inproc://workers");
- struct example *buffer;
- int size = zmq_recv(receiver,buffer,sizeof(struct example),0);
- if(size==-1)
- {
- printf("Error in Receiving \n");
- }
- struct example *obj = (struct example*)malloc(sizeof(struct example*));
- memcpy(obj,buffer,sizeof(struct example));
- printf("The values received are %d %d %d %d %s\n",obj->a,obj->b,obj->obj2->b,obj->obj2->c,obj->obj2->str);//
- zmq_close (receiver);
- }
- int main (void)
- {
- void *context = zmq_ctx_new ();
- // Socket to talk to workers
- void *workers = zmq_socket (context, ZMQ_PAIR);
- zmq_bind (workers, "inproc://workers");
- struct example *obj = (struct example*)malloc(sizeof(struct example*));
- obj->a=1000;
- obj->b=5000;
- obj->obj2 = (struct sample*)malloc(sizeof(struct sample*));
- strcpy(obj->obj2->str,"HelloWorld");
- obj->obj2->b=100;
- obj->obj2->c=500;
- // Launch pool of worker threads
- int thread_nbr;
- pthread_t worker;
- pthread_create (&worker, NULL, worker_routine, context);
- int size;
- size = zmq_send (workers,obj,sizeof(struct example),0);
- if(size==-1)
- {
- printf("Error in sending");
- }
- // We never get here, but clean up anyhow
- zmq_close (workers);
- zmq_ctx_destroy (context);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment