grand-central-dispatch

Suggested resources for learning about blocks in Snow Leopard

Now that there is no NDA, what are some good suggested resources for learning about blocks under Snow Leopard? ...

How different programming languages use closures?

To my knowledge, combined with the knowledge of others, among the mainstream languages Objective C C# VB.net Java Python Ruby Javascript Lisp Perl have closures and anonymous functions. Plain C/C++ doesn't have either of those. Do closures in these languages have the same semantics? How important are they for everyday programming? ...

Concurrent network client in Cocoa

I'm trying to work out in my head the best way to structure a Cocoa app that's essentially a concurrent download manager. There's a server the app talks to, the user makes a big list of things to pull down, and the app processes that list. (It's not using HTTP or FTP, so I can't use the URL-loading system; I'll be talking across socket c...

Dispatch queues: How to tell if they're running and how to stop them.

I'm just playing around with GCD and I've written a toy CoinFlipper app. Here's the method that flips the coins: - (void)flipCoins:(NSUInteger)nFlips{ // Create the queues for work dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NUL...

How does Grand Central Dispatch really use the operating system?

I have a solid idea how GCD works, but I want to know more about the touted "operating system management" internals. It seems almost every technical explanation of how Grand Central Dispatch works with the "Operating System" is totally different. I'll paraphrase some of my findings. "It's a daemon that's global to the OS that dis...

One code base for Snow Leopard and Leopard

Background I'm a developer who's in the throes of building an application for the Mac. I'm about to get my hands on Snow Leopard. Until now I've been building on Leopard. I've only been doing Cocoa development for about a year not very intensely. I've read a lot about Snow Leopard and Grand Central Dispatch in particular. I'm aware tha...

How to parallelize Sudoku solver using Grand Central Dispatch?

As a programming exercise, I just finished writing a Sudoku solver that uses the backtracking algorithm (see Wikipedia for a simple example written in C). To take this a step further, I would like to use Snow Leopard's GCD to parallelize this so that it runs on all of my machine's cores. Can someone give me pointers on how I should go a...

Does pthreads provide any advantages over GCD?

Having recently learned Grand Central Dispatch, I've found multithreaded code to be pretty intuitive(with GCD). I like the fact that no locks are required(and the fact that it uses lockless data structures internally), and that the API is very simple. Now, I'm beginning to learn pthreads, and I can't help but be a little overwhelmed wit...

Why is my computer not showing a speedup when I use parallel code?

So I realize this question sounds stupid (and yes I am using a dual core), but I have tried two different libraries (Grand Central Dispatch and OpenMP), and when using clock() to time the code with and without the lines that make it parallel, the speed is the same. (for the record they were both using their own form of parallel for). T...

Number of threads and thread numbers in Grand Central Dispatch

I am using C and Grand Central Dispatch to parallelize some heavy computations. How can I get the number of threads used by GCD? Also is it possible to know on which thread a piece of code is currently running on? Basically I'd like to use sprng (parallel random numbers) with multiple streams and for that I need to know what stream id to...

Could Grand Central Dispatch (`libdispatch`) ever be made available on Windows?

I’m looking into multithreading, and GCD seems like a much better option than manually writing a solution using pthread.h and pthreads-win32. However, although it looks like libdispatch is either working on, or soon going to be working on, most newer POSIX-compatible systems… I have to ask, what about Windows? What are the chances of lib...

GCD and AppleEvents / NSOperationQueue and AppleScript

As i understood, the threads provided by GCD do have a runloop but no source/port. Now i use some of methods that call AppleScripts thru AppleEvents inside an NSOperationQueue. And sometimes my app crashes with following stacktrace. my questions: Usage of AppleScript inside a NSInvocationOperation or NSBlockOperation Usage of AppleE...

How do I specify the block object / predicate required by NSDictionary's keysOfEntriesPassingTest ?

For learning (not practical -- yet) purposes, I'd like to use the following method on an NSDictionary to give me back a set of keys that have values using a test I've defined. Unfortunately have no idea how to specify the predicate. NSDictionary keysOfEntriesPassingTest: - (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL...

NSThread vs. NSOperationQueue vs. ??? on the iPhone

Currently I'm using NSThread to cache images in another thread. [NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObject:image]; Alternatively: [self performSelectorInBackground:@selector(cacheImage:) withObject:image]; Alternatively, I can use an NSOperationQueue NSInvocationOperation *invOperation = [[N...

GCD / block scoping in Objective-C++

I'm using GCD to speed up some calculations in a few C++ templates I'm working on. I've already done it successfully for several functions, but now I'm trying to get it to work for a member function and I've encountered this weird scoping problem. The code looks something like this: inline void op::factorOutGaussian(const double *x, c...

How to "break" out of dispatch_apply()?

Is there a way to simulate a break statement in a dispatch_apply() block? E.g., every Cocoa API I've seen dealing with enumerating blocks has a "stop" parameter: [array enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *stop) { if ([obj isNotVeryNice]) { *stop = YES; // No more enumerating! } else { NSLog(...

Using grand central dispatch in Linux

Is this possible, since Apple has open sourced the code (libdispatch?) I'm bit confused as to how one can make use of this. Is this like a library with an API that any application can make use of, or a OS feature built into Mac OS X? Can application built "for" Macs and iPhone (with iOS 4) alone make use of this library? I assume one ha...

GCD and RunLoops

Hi everyone. In my app I add an CFMachPortRef (via CFMachPortCreateRunLoopSource) to a threads CFRunLoop Now i was asking myself, can this be done using GCD? Let's say instead of spawning my own NSThread and add the created CFRunLoopSourceRef to its run loop via CFRunLoopAddSource, add the event port to a dispatch's runloop? I think t...

How can I be notified when a dispatch_async task is complete?

I have a asynchronous task like so: dispatch_async(dispatch_get_main_queue(), ^{ myAsyncMethodsHere; }); Is there a way to be notified when the background task is complete? Or to call a method upon completion? I've read through the documentation and have looked into dispatch_after, but it seems to be more designed to dispatch t...

Performance test: sem_t v.s. dispatch_semaphore_t and pthread_once_t v.s. dispatch_once_t

I wanted to know what would be better/faster to use POSIX calls like pthread_once() and sem_wait() or the dispatch_* functions so I created a little test and am surprised at the results (questions and results are at the end). In the test code I am using mach_absolute_time() to time the calls. I really dont care that this is not exactl...