title: How to use multiple threads for zlib compression
body: <p>I have a large chunk of data (around 2 GB) that needs to be compressed using zlib (deflate()). I am currently reading 500 kb of data at 1 go, compressing it and writing it to my output file.</p>
  
  <p>With 1 thread, everything is fine. The data is compressed and I am able to write it and uncompress it back.</p>
  
  <p>With 2 threads, the process hangs in deflate() call.</p>
  
  <p>Here is the outline of the function that is called by my 2 zlib comp threads.</p>
  
  <pre><code>static z_stream z_str;
  
  zlib_compress(...., bool last, bool first)
  {
  
      if (first)
          deflateInit(&amp;z_str, Z_DEFAULT_COMPRESSION);
  
      if (last)
          flush = Z_FINISH;
      else
          flush = Z_SYNC_FLUSH;
  
  ....
  ....
      status = deflate(&amp;z_str, flush);
  ...
  ...
      if (last)
          deflateEnd(&amp;z_str);
  
  }
  </code></pre>
  
  <p>As I understand, both the calls are referring to the same zstream while calling deflate(), which is resulting in undesired behaviour.</p>
  
  <p>I tried to take z_str as a local variable and modified the code accordingly. But while uncompressing, it is assuming 512 as the total size of the file which is actually just the first chunk of data.</p>
  
  <p>Any idea how to achieve this?</p>