[libdispatch-dev] Replacement for dispatch_get_current_queue

Allan Odgaard lists+libdispatch at simplit.com
Fri Apr 19 19:33:07 PDT 2013

On Apr 20, 2013, at 4:13, Thomas Clement <tclementdev at gmail.com> wrote:

> On 19 avr. 2013, at 22:48, Adam Ernst <adamjernst at fb.com> wrote:
>> The design of dispatch_get_current_queue is clearly problematic, as its docs indicate. I've heard rumors the function will be deprecated and removed soon.
>> […]
> Take a look at dispatch_queue_get_specific and dispatch_queue_set_specific.

I have another use-case where objects can only be accessed from a single thread.

For example:

  class MyClass
    void set_foo (Foo* foo);

  std::shared_ptr<MyClass> create_object ()
    dispatch_queue_t original_queue = dispatch_get_current_queue();

    std::shared_ptr<MyClass> res(new MyClass);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      Foo* foo = new Foo; // could be expensive
      dispatch_async(original_queue, ^{

    return res;

One solution is to pass the current queue to create_object(), which normally would be the main queue, however, when running tests, it’s the testing framework that decides which queue a test executes in, so tests would have to create their own “inner” queue and run the actual code in that, so that they have a valid queue to pass to create_object().

Another solution would be to allow set_foo() to be executed on arbitrary threads. This however will make the implementation of MyClass harder, and it would basically only be for the benefit of our tests, so I don’t think this cost/extra complexity is justified.

Probably the first solution is the best, but I’d be interested in hearing others’ take on this.

More information about the libdispatch-dev mailing list