title: Multithreading and structs with ZeroMQ
body: <p>I'm trying to figure out exactly why I can't successfully pass a struct through zthread_fork using ZeroMQ. </p>
  
  <p>The <a href="http://czmq.zeromq.org/manual:zthread" rel="nofollow">documentation</a> for zthread_fork says that it accepts the following as arguments:</p>
  
  <pre><code>zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args);
  </code></pre>
  
  <p>Here's my function call:</p>
  
  <pre><code>void *client = zthread_fork ( ctx, client_thread, (void *)&amp;my_struct );
  </code></pre>
  
  <p>Here's my function prototype:</p>
  
  <pre><code>static void client_thread ( zctx_t *ctx, void *args )
  </code></pre>
  
  <p>It seems fairly simple to me - create a struct and pass a (void *) pointer to zthread_fork(), which client_thread is expecting. </p>
  
  <p>I get a set of error messages when I compile:</p>
  
  <pre><code>$gcc ft_test3_verbose.c -o ft_test3_verbose -L/usr/local/lib -lczmq &gt; err_out
  ft_test3_verbose.c: In function ‘client_thread’:
  ft_test3_verbose.c:18:31: error: invalid initializer
   struct arg_struct input = (struct arg_struct *)args;
                             ^
  ft_test3_verbose.c:20:46: error: invalid type argument of ‘-&gt;’ (have ‘struct arg_struct’)
   printf( "Here's some stuff: %d \n", input-&gt;data );
                                            ^
  ft_test3_verbose.c:21:53: error: invalid type argument of ‘-&gt;’ (have ‘struct arg_struct’)
   printf( "Here's a different stuff: %s \n", input-&gt;test_str );
                                                   ^
  ft_test3_verbose.c: In function ‘main’:
  ft_test3_verbose.c:123:29: warning: passing argument 2 of ‘zthread_fork’ from incompatible pointer type [-Wincompatible-pointer-types]
       zthread_fork ( ctx, client_thread, (void *)&amp;my_struct );
                           ^
  In file included from /usr/local/include/czmq_library.h:175:0,
               from /usr/local/include/czmq.h:30,
               from ft_test3_verbose.c:2:
  /usr/local/include/zthread.h:39:5: note: expected ‘void (*)(void *, zctx_t *, void *) {aka void (*)(void *, struct _zctx_t *, void *)}’ but argument is of type ‘void (*)(zctx_t *, void *) {aka void (*)(struct _zctx_t *, void *)}’
       zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args);
       ^
  </code></pre>
  
  <p>Here's my code (I haven't included the server_thread() function because it's identical to the example server_thread() from the <a href="http://zguide.zeromq.org/c:fileio3" rel="nofollow">ZMQ FileIO example</a>:</p>
  
  <pre><code>#include &lt;zmq.h&gt;
  #include &lt;czmq.h&gt;
  #define CHUNK_SIZE 250000
  #define PIPELINE 10
  
  struct arg_struct {
      int data;
  char *test_str;
  
  };
  
  static void client_thread ( zctx_t *ctx, void *args )
  {
      void *pipe = NULL;
      void *dealer = zsocket_new (ctx, ZMQ_DEALER);
      zsocket_connect (dealer, "tcp://127.0.0.1:6000");
  
      struct arg_struct input = (struct arg_struct *)args;
  
      printf( "Here's some stuff: %d \n", input-&gt;data );
      printf( "Here's a different stuff: %s \n", input-&gt;test_str );
      zstr_send( pipe, "OK" );
  }
  
  int main (void)
  {
      zctx_t *ctx = zctx_new (); 
  
      struct arg_struct my_struct = { 0, "testing" };
  
      zthread_fork (ctx, server_thread, NULL);
      void *client = 
          zthread_fork ( ctx, client_thread, (void *)&amp;my_struct );
      char *string = zstr_recv (client);
      free (string);
      zctx_destroy (&amp;ctx);
      return 0;
  }
  </code></pre>
  
  <p>Does anyone have ideas as to what I'm doing wrong? Help is much appreciated</p>