2

im developing an app, which uses some framework to draw 3D staff via openGL. This framework requires me to call draw() method from exact the same Thread.

So i created a serial DispatchQueue and started CADisplayLink in it, calling draw() at 60FPS. There are few other methods that i have to call from this exact thread, like start() and stop(). This makes queues perfect solution to me.

As you may know DispathQueue does not guaranteed to execute every task on the same thread. Which is quite stressful for me, as it may break my app.

I don't really like the idea to create NSThread and implement my own queue on it.

Are there any way to bind DispatchQueue to exact Thread? Maybe NSOperationQueue can be bound?

3
  • "As you may know DispathQueue does not guaranteed to execute every task on the same thread. " especially not, when you create different background threads implicitly for each task.
    – holex
    Commented Sep 18, 2018 at 15:21
  • The only queue that is guaranteed to run on a particular thread is the main queue, which always runs on the main thread.
    – Paulw11
    Commented Sep 18, 2018 at 19:27
  • That’s true, but I don’t like to block main thread with my DisplayLink
    – DoN1cK
    Commented Sep 18, 2018 at 20:57

1 Answer 1

6

As Apple Documentation says:

When it comes to adding concurrency to an application, dispatch queues provide several advantages over threads. The most direct advantage is the simplicity of the work-queue programming model. With threads, you have to write code both for the work you want to perform and for the creation and management of the threads themselves. Dispatch queues let you focus on the work you actually want to perform without having to worry about the thread creation and management. Instead, the system handles all of the thread creation and management for you. The advantage is that the system is able to manage threads much more efficiently than any single application ever could. The system can scale the number of threads dynamically based on the available resources and current system conditions. In addition, the system is usually able to start running your task more quickly than you could if you created the thread yourself.

In simple words, you either work with dispatch queues, simply creating them and sending work to them, OR you work with NSThreads and NSRunLoops, creating them, setting them up, sending work to them, and possibly stopping them.

In detail:

NSThread / NSRunLoop

Creation:

self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMainRoutine) object:nil];
[self.thread start];

Start / management:

- (void)threadMainRoutine
{
    // Set the runLoop variable, to signal this thread is alive
    self.runLoop = [NSRunLoop currentRunLoop];

    // Add a fake Mach port to the Run Loop, to avoid useless iterations of the main loop when the
    //  thread is just started (at this time there are no events added to the run loop, so it will
    //  exit immediately from its run() method)
    [self.runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    //--- Thread main loop
    while (thread_KeepRunning)
    {
        // Run the run loop. This function returns immediately if the RunLoop has nothing to do.
        //  NOTE: THIS STATEMENT:
        //      [self.runLoop run];
        //      DOES NOT WORK, although it is equivalent to CFRunLoopRun();
        CFRunLoopRun();
    }

    // Unset the runLoop variable, to signal this thread is about to exit
    self.runLoop = nil;
}

Adding work to be performed on it:

[self performSelector:@selector(mySelector:) onThread:myThread withObject:myObject waitUntilDone:YES];

Shutdown:

- (void)stop
{
    if (self.thread) {
        while (self.thread.isExecuting) {
            thread_KeepRunning = NO;
            CFRunLoopStop([self.runLoop getCFRunLoop]);
            [NSThread sleepForTimeInterval:0.1f];
        }
    }

    self.runLoop = nil;
    self.thread = nil;
}

Dispatch Queue

Creation:

dispatch_queue_t myQueue = dispatch_queue_create("My Queue", DISPATCH_QUEUE_SERIAL);

Start:

dispatch_resume(myQueue);

Adding work to be performed on it:

dispatch_async(myQueue, (void)^ {
    // put the work into this block
});

Shutdown:

dispatch_suspend(myQueue);
myQueue = nil;

In addition, Apple Documentation says that

Because Grand Central Dispatch manages the relationship between the tasks you provide and the threads on which those tasks run, you should generally avoid calling POSIX thread routines from your task code. If you do need to call them for some reason, you should be very careful about which routines you call

So: if you use dispatch queues, don't mess with threads.

2
  • Thanks for the answer. So i got the main idea - you can't have DispatchQueue bounded to exactly one thread
    – DoN1cK
    Commented Oct 25, 2018 at 10:30
  • dispatch_resume(myQueue); ???
    – Mojo66
    Commented Jan 18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.