Purge the contents of a data container in real tim

2020-05-05 17:42发布

My problem is best explained by the architecture below enter image description here

Basically, I have to purge the contents of an NSMutableData object (or any other object, for that matter) in real time, i.e., I can't block its containing thread. Is there a way/API to do this?

update: As mentioned in the comments below.. i'm receiving audio packets in real time to the main thread and immediately route it to a dedicated audio pool thread.. (the reason why i got so many threads is b/c i have no control over the reception of the incoming packets.. they come in real time + the rate of playback is a lot slower than the rate of receiving data.. thus the seperate audio pool thread and the reader thread)

1条回答
仙女界的扛把子
2楼-- · 2020-05-05 18:04

Circular buffers aka ring buffers work nicely with NSMutableData since you can use the byte array directly as the buffer.

Looking at the design, I don't see why you need so many buffers. There appears to be one thread and two buffers too many. Why not just have one ring buffer, fill that up (it's 'self purging') and read from it in a separate thread? Is the design yours (ie can be changed) or has it been imposed on you?

In any event, try using a ring buffer for your audio pool.

查看更多
登录 后发表回答