views:

421

answers:

1

In Cocoa, I've setup two NSThreads, one producer and one consumer. The producer appends data to an NSMutableData, and the receiver opens an NSInputStream from that data and reads in chunks.

The producer thread writes a lot faster than the consumer processes, which is OK. But the producer only produces a finite amount of work, then exits. I'd like for the consumer thread to handle the NSStreamEventEndEncountered event. How can I signal this when the NSInputStream comes from an NSMutableData?

Should I just make the producer send a series of bytes with a magical number signifying an end of stream?

+1  A: 

The producer appends data to an NSMutableData, and the receiver opens an NSInputStream from that data and reads in chunks.

That won't work. From the -initWithData: documentation:

The contents of data are copied.

Meaning, it will copy out whatever's in the data object at that time, not follow the contents of the data object as you add more data to it.

(The same goes for +inputStreamWithData:.)

Try a pipe instead.

Peter Hosey