0

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)

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
  • 1
    what does "purge the content" mean? – deleted_user Sep 17 '12 at 16:01
  • this is a basic buffering question – deleted_user Sep 17 '12 at 16:01
  • @stackmonster purge the content basically means that I want to clear data that has already been sent to it. The trick here is purging and writing data *at the same time in the same thread*.. i don't think this is that basic of a question – abbood Sep 17 '12 at 16:46
  • Its pretty basic. This is not a great design – deleted_user Sep 17 '12 at 16:51
  • @stackmonster and that's not a great comment, either. The question is entirely reasonable and the diagram explains it well enough. – Tim Sep 17 '12 at 18:07
  • its just buffering. If you have the opportunity to have such a diagram, I would expect to know how to implement it. – deleted_user Sep 17 '12 at 18:09
  • @stackmonster so answer him then; earn some rep for an easy question rather than just being a grumpy know-it-all. – Tim Sep 17 '12 at 18:14
  • I guess what Im saying is, if I could close this question I would, this is not a question. Its someone saying - I have this design...code it for me. – deleted_user Sep 17 '12 at 18:22
  • @stackmonster I actually agree that the design is a bit awkward, but his question is really "How can I make a buffer using NSMutableData in a thread-safe way within the constraints of my design?" It's definitely not "code it for me." Anyway, too many comments here already. – Tim Sep 17 '12 at 18:41

1 Answers1

0

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.

Tim
  • 5,024
  • 2
  • 30
  • 58
  • I've considered ring buffers, the problem with ring buffers is that in order for it to work, data in the buffer must be produced and consumed at pretty much the same rate -> which kind of addresses the second part of your question, which is why am i having too many threads? the reason being is that i'm receiving data in real time.. so i can't block it or constrain it in any way (and i pretty much need a dedicated thread for it, i cannot have that thread do anything else that can slow down the reception of the real time data, so the MainThread is no good as it has to do UI updates etc) --> – abbood Sep 18 '12 at 06:59
  • <-- the reader thread is more like a controller of the flow of the information.. it continuously asks for data from the audio pool thread.. and stops asking when the audio queue ring buffer is full.. so in short.. simply replacing the audio pool with a ring buffer won't address my real time parameters (i will update the question) – abbood Sep 18 '12 at 07:01