Re: Can I create a thread with a runloop and a dispatch queue?
Re: Can I create a thread with a runloop and a dispatch queue?
- Subject: Re: Can I create a thread with a runloop and a dispatch queue?
- From: Ken Thomases <email@hidden>
- Date: Thu, 03 Oct 2013 13:58:16 -0500
On Oct 3, 2013, at 1:46 PM, Kyle Sluder wrote:
> On Thu, Oct 3, 2013, at 10:55 AM, Jens Alfke wrote:
>> The main thread of a Cocoa app has a runloop (of course) and also the
>> main GCD dispatch queue. This is very handy because it means tasks on
>> that thread can be scheduled either using the runloop (NSTimer or
>> delayed-perform) or using GCD (dispatch_async, dispatch_sync).
>>
>> But background threads don’t seem to have the same property. If I create
>> a thread using NSThread, it supports a runloop, but I don’t see any API
>> for getting or creating a dispatch queue that runs in conjunction with
>> the runloop. Did I miss something?
>
> There is magic sauce in CFRunLoop that runs the main queue as part of
> processing the runloop. All other queues are managed by code running on
> a thread managed by libdispatch. The main queue is a special case; it
> doesn't make sense for this relationship to exist for other queues.
That said, it's simple enough to write a category on NSThread such that you can ask a thread to run a block and it will do so when its run loop is run. Basically, it would leverage -[NSObject performSelector:onThread:…] and the block would run in the same circumstances as a selector would.
Not as much functionality as a dispatch queue, but might suffice for your needs.
There is the question of why you want to dedicate a thread to running such requests (and other run loop sources) rather than using the GCD worker threads.
Regards,
Ken
_______________________________________________
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