Re: Thread synchronization via queue
Re: Thread synchronization via queue
- Subject: Re: Thread synchronization via queue
- From: Roland King <email@hidden>
- Date: Thu, 17 Jan 2013 19:02:41 +0800
There's quite a few things you can try in GCD.
One of them (and this is designed in email so I'm sure this is going to get holes poked all over it but might make for a fun discussion) is to make one worker dispatch_queue on which all of your work happens, that protects your NSMutableData from multiple access. That's a serial queue. You make two more queues, a reader and a writer and you use dispatch_set_target_queue on each of them to your 'real' queue which means they will send their work there.
The code which wants to read tosses a read block with whatever fragment handler you have onto the read queue, it finds data, processes as much as it wants. it then tosses another similar read block onto the same queue to process the next chunk. If however the read it just did depleted the data to zero and there's no more, before it exists it suspends its own read queue leaving the block it just queued waiting.
The writer does something similar. When there's data to write it tosses a write block onto the write queue. That appends data to the NSMutableData. Also, if it sees that the data was empty before it appended, it resumes the read queue before exiting the write block and the next reader block will start.
You start with the data empty and the read queue suspended with one read block on it. As soon as the writer writes, the read queue unsuspends and you start reading.
That idea probably needs refining and it does depend exactly what you currently have on the other side and whether it would work as callback blocks.
There's probably ways to do that with dispatch_semaphore too and probably if you try hard with one of the dispatch_sources.
On 17 Jan, 2013, at 5:40 PM, Rick Mann <email@hidden> wrote:
> I've got a situation where I have a complicated process that periodically pulls some bytes out of an NSMutableData. Eventually, it gets to the end of the NSMutableData. At that point, I need the process to block while it waits for a separate process (thread) to give it more data. What I need is a semaphored queue for a producer-consumer type of relationship.
>
> I don't think there's any way to pull this off using GCD, is there? I can't change the complex process to work in discrete chunks. It needs to chug through the data until it reaches a stopping point, and that might require several waits on more data.
>
> --
> Rick
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden