Does GCD assure that all the blocks working in the same queue are always working in a same thread?(About ABAddressBookRef)

Does GCD assure that all the blocks working in the same queue are always working in a same thread?

If I create a dispatch queue and dispath_async blocks to this queue, does all the blocks that dispatch to that queue works in the same thread?

Since I'm working on a project that uses ABAdrressbook Framerowk and the document says that ABAddressBookRef and ABRecordRef can't be used between threads, so I wonder if all the blocks in the queue are in the same thread, I can create only one AddressBookRef for that queue.

-------------Problems Reply------------

The only queue bound to a specific thread is the main queue, which is bound to the main (UI) thread.

If the only requirement is not to concurrently access the object, using a serial queue should work fine.

If the object instead relies on thread-local state, you will have to force all manipulation to a specific thread. The easiest would be to target your serial queue to the main thread, but that only works if you know you're not going to be stuck for long in the block; otherwise, you will hang your UI. In that case, you'll have to create your own handler thread and send the work over there.

  • dispatch_queue_create(3) Mac OS X Manual Page

Queues are not bound to any specific thread of execution

There is no guarantee that all blocks sent to a serial queue are sent to the same thread. And I couldn't find any source code for combination of ABAddressBookCreate with GCD Serial Queue...

When the documentation says that something can't be used between threads, it only means that the API can't be used concurrently from different threads at the same time. The API itself doesn't remember anything special about the thread calling it or something like that and forcing it to be the same each time.

Previous to GCD you would serialize access to a shared resource with @synchronize. As you suggest yourself, creating a queue for this is another way of serializing access to this resource which is more efficient.

Category:thread safety Views:0 Time:2011-08-09

Related post

  • Can I assume that blocks scheduled to run on a serial queue will all run on the same thread? 2011-03-27

    I'm building an app for Mac OS 10.6 that will use OpenGL. I'd like to offshore the rendering to a secondary dispatch queue (instead of the main thread). From what I understand, I need to use a thread-local OpenGL context to do my graphics work. My pl

  • In GCD, is there a way to tell if the current queue is concurrent or not? 2011-08-18

    In GCD, is there a way to tell if the current queue is concurrent or not? I'm current attempting to perform a delayed save on some managed object contexts but I need to make sure that the queue the code is currently executed on is thread-safe (in a s

  • Is there a way to assure FIFO (first in, first out) behavior with Task Queues on GAE? 2012-04-02

    Is there a way to assure FIFO (first in, first out) behavior with Task Queues on GAE? GAE Documentation says that FIFO is one of the factors that affect task execution order, but the same documentation says that “the system's scheduling may 'jump' ne

  • java try catch block not working using reflection - have no idea why, also maybe a thread error 2011-04-21

    writing a system in netbeans rcp - not sure if this matters, i just don't trust the rcp We're approaching crunch time on our system and the following error keeps happening (sometimes it happens after 10 minutes, sometimes it runs for 2 days and then

  • How to dispatch a block with parameter on main queue or thread 2011-09-09

    Blocks are awesome. Because I thought I understood them, I wanted to up the ante and use them in a little more complex situation. Now these blocks are kicking me in the face, and I'm trying to break it down into comprehensible pieces. Say I have two

  • My Produce Consumer Hangs 2010-06-18

    Please copy the program below and try running in your IDE. It's a simple Produce Consumer implementation - it runs fine when I use one Producer and one Consumer thread but fails when using 2 each. Please let me know the reason why this program hangs

  • Does puting a block on a sync GCD queue locks that block and pauses the others? 2011-09-08

    I read that GCD synchronous queues (dispatch_sync) should be used to implement critical sections of code. An example would be a block that subtracts transaction amount from account balance. The interesting part of sync calls is a question, how does t

  • Here i am trying to call my ASIHTTP request in a GCD . But Completion block and failed blocks are not executing 2011-10-09

    Here i am trying to call my ASIHTTPRequest in a GCD. Sometimes the comletion blocks and failed blocks are not executing. What i want to do is, after this request finished, i have to use the returned data in a another ASIHTTPRequest. So how to improve

  • How do blocks differ from normal methods and functions in Objective-C? 2010-08-23

    What is the advantage of using blocks over normal methods and functions in Objective-C? I've read the documentation, but I can't find specific uses of blocks instead of other language features. I'm sure that I've missed something, so could someone ex

  • Blocks get fired one after another when using dispatch_group_async 2011-01-06

    URLs in the array are called one after another. Should it not be called all at once, like nsoperationqueue? Please help me here, Thanks - (void) allTasksDone { NSLog(@"DONE"); } - (void) callMultiple { dispatch_queue_t myQueue = dispatch_queue_create

  • Using a variable across two blocks 2011-01-12

    I have added a method -dct_asynchronousTaskWithWorkBlock:completionBlock: to NSManagedObjectContext which runs the work block in another GCD queue and then runs the completion block on the main thread. The method does the following: Creates a second

  • GCD Poor Performance 2011-02-20

    As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my se

  • Event Driven IO And Blocking vs NonBlocking 2011-04-27

    Can someone explain to me how event-driven IO system calls like select, poll, and epoll relate to blocking vs non-blocking IO? I don't understand how related -- if at all, these concepts are --------------Solutions------------- The select system call

  • dispatch_queue_t still blocking main thread 2011-06-27

    I want to fire off a method and have it run in the background - I do not care what really happens to it after it is started. So in my main viewDidLoadMethod I have all of my usually code and this: dispatch_queue_t newImages = dispatch_queue_create("l

  • GCD, Threads, Program Flow and UI Updating 2011-09-03

    I'm having a hard time figuring out how to put this all together. I have a puzzle solving app on the mac. You enter the puzzle, press a button, and while it's trying to find the number of solutions, min moves and such I would like to keep the UI upda

  • Is a GCD dispatch queue enough to confine a Core Data context to a single thread 2011-10-10

    I'm beginning to think the answer to my question is 'No', but I'm still confused and uncertain about this. So please confirm. I've already learned the need to be careful when using Core Data with multiple threads. NSManagedObjectContext objects must

  • Background task block function not finishing 2012-01-10

    I'm working on an iphone app that occasionally fires a task in the background to rearrange some data and upload it to a server. I've used a lot of the principles from Grand Central Dispatch (GCD) with CoreData to get things running, since I'm editing

  • GCD and async NSURLConnection 2012-01-18

    I know that if I create an NSURLConnection (standard async one), it will call back on the same thread. Currently this is on my main thread. (work fine too). But i'm now using the same code for something else, and I need to keep my UI snappy.... If i

  • dealloc called on background GCD queue crashing app built with ARC 2012-01-19

    I have a view controller that downloads an asset in a background GCD queue. I pass my downloading function a callback block to execute once the download is finished, and it always executes this block on the main thread. The problem occurs if my view

Copyright (C), All Rights Reserved.

processed in 0.154 (s). 11 q(s)