Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to release a GCD dispatch queue property?

I'm using a dispatch_queue which is accessed through a property of its owner, like this:

@property (nonatomic, assign) dispatch_queue_t queue;

Note the assign keyword. The queue is used throughout the objects life and thus owned by the object. I release the queue when the owning object is deallocated:

-(void)dealloc
{
    dispatch_release(self.queue);
    self.queue = nil;
}

How do I properly release this? Would using retain/release work?

What happens if there is stuff pending/running on the queue while calling release?

like image 360
Martin Wickman Avatar asked Apr 01 '11 08:04

Martin Wickman


People also ask

How does queue dispatch work?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

What is serial queue in GCD?

Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue. The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue.

What is GCD in swift5?

Grand Central Dispatch (GCD) is a low-level API for managing concurrent operations. It can help improve your app's responsiveness by deferring computationally expensive tasks to the background. It's an easier concurrency model to work with than locks and threads.

How many types of dispatch queues are there?

There are two types of dispatch queues, serial dispatch queues and concurrent dispatch queues.


1 Answers

The following is stolen from the developer documentation:

Dispatch queues and other dispatch objects are reference-counted data types. When you create a serial dispatch queue, it has an initial reference count of 1. You can use the dispatch_retain and dispatch_release functions to increment and decrement that reference count as needed. When the reference count of a queue reaches zero, the system asynchronously deallocates the queue.

When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.

Note: You do not need to retain or release any of the global dispatch queues, including the concurrent dispatch queues or the main dispatch queue. Any attempts to retain or release the queues are ignored.

So anywhere you would use -retain use dispatch_retain and anywhere you would use -release use dispatch_release.

Dispatch queues follow the same general memory management conventions as objective-c objects. And they won't be dealloc'ed until all blocks queued are finished.

If you do want a way to shut down a dispatch queue: There's no way to cancel all enqueued blocks via any sort of API, so they always must run to completion. One way to expedite this process is to have a BOOL variable in the class managing the dispatch queue: _isValid. When you want to shut down the queue, you can set _isValid to NO. All blocks submitted to the queue should first check _isValid before doing any work.

A side comment: It may be more appropriate to use NSOperationQueue. See Chris Hanson's blog post.

like image 83
Stephen Poletto Avatar answered Sep 29 '22 14:09

Stephen Poletto