Guest User

ZeroMQ ZMQ_PAIR object pointer address sharing between threa

a guest
Dec 20th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. //#include "zhelpers.h"
  2. #include <zmq.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6.    
  7. struct sample
  8. {
  9.     int b;
  10.     int c;
  11.     char str[32];
  12. }; 
  13.    
  14. struct example
  15. {
  16.     int a;
  17.     int b;
  18.         struct sample *obj2;
  19. };
  20. static void *
  21. worker_routine (void *context) {
  22.     //  Socket to talk to dispatcher
  23.     void *receiver = zmq_socket (context, ZMQ_PAIR);
  24.     zmq_connect (receiver, "inproc://workers");
  25.  
  26.     struct example *buffer;
  27.     int size = zmq_recv(receiver,buffer,sizeof(struct example),0);
  28.     if(size==-1)
  29.     {
  30.         printf("Error in Receiving \n");
  31.     }
  32.    
  33.    
  34.    
  35.     struct example *obj = (struct example*)malloc(sizeof(struct example*));
  36.     memcpy(obj,buffer,sizeof(struct example));
  37.    
  38.    
  39.     printf("The values received are %d %d %d %d %s\n",obj->a,obj->b,obj->obj2->b,obj->obj2->c,obj->obj2->str);//
  40.     zmq_close (receiver);
  41.    
  42. }
  43.  
  44. int main (void)
  45. {
  46.     void *context = zmq_ctx_new ();
  47.  
  48.     //  Socket to talk to workers
  49.     void *workers = zmq_socket (context, ZMQ_PAIR);
  50.     zmq_bind (workers, "inproc://workers");
  51.  
  52.     struct example *obj = (struct example*)malloc(sizeof(struct example*));
  53.     obj->a=1000;
  54.     obj->b=5000;
  55.     obj->obj2 = (struct sample*)malloc(sizeof(struct sample*));
  56.     strcpy(obj->obj2->str,"HelloWorld");
  57.     obj->obj2->b=100;
  58.     obj->obj2->c=500;
  59.     //  Launch pool of worker threads
  60.     int thread_nbr;
  61.         pthread_t worker;
  62.         pthread_create (&worker, NULL, worker_routine, context);
  63.    
  64.     int size;
  65.     size = zmq_send (workers,obj,sizeof(struct example),0);
  66.     if(size==-1)
  67.     {
  68.         printf("Error in sending");
  69.     }
  70.    
  71.     //  We never get here, but clean up anyhow
  72.     zmq_close (workers);
  73.     zmq_ctx_destroy (context);
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment