3

I am using the zlib C library to decompress data received from a network stream, and I have two streams running in two separate NSThreads. As per zlib documentation, decompressing two different zlib streams in two threads requires zalloc and zfree to be thread safe. I am currently passing Z_NULL (my inflateinit code below)

zStream.zalloc = Z_NULL;
zStream.zfree = Z_NULL;
zStream.opaque = Z_NULL;
zStream.avail_in = 0;
zStream.next_in = Z_NULL;
int status = inflateInit(&zStream);
if (status != Z_OK)
    DLog(@"zlib setup error");
else
    DLog(@"zlib setup ok");
});

Does anyone have experience with using zlib for compressing multiple streams in separate threads? Has anyone seen a thread safe implementation of zalloc and zfree?

R.S
  • 321
  • 3
  • 15

1 Answers1

4

malloc() and free() on Mac OS X are already thread safe. So just initialize the structure with Z_NULL as you're doing and you'll be fine.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Thanks Mark. I am using them as is but getting a Zlib deflation error (-3). If the problem is not with zlib, it might be with my network streams themselves. Let me investigate this further and once I confirm that the problem is not with zlib, I will mark this answer as correct. – R.S Oct 13 '12 at 17:48
  • `-3` is a data error. That means the data you are feeding `inflate()` is invalid. – Mark Adler Oct 13 '12 at 18:26
  • yes the problem is with the input to zlib, not with zlib code. – R.S Nov 05 '12 at 21:07