Grand Central Dispatch goes Open Source

Feel free to talk about anything and everything in this board.
Post Reply
Altimit01
Eschaton Dev
Eschaton Dev
Posts: 2108
Joined: Sun Apr 15, 2007 7:43 pm

Grand Central Dispatch goes Open Source

Post by Altimit01 » Fri Sep 11, 2009 12:37 pm

Apple has open sourced the Grand Central Dispatch library as libdispatch under the Apache 2.0 license. GCD provides a way to make parallel programming very easy for developers and is most useful for the C family of languages. It combines a proposed concept in C called blocks (or closures) which are somewhat similar to function pointers (for use with callbacks) and make functional programming a lot easier. These blocks are then passed to GCD which can queue synchronous and asynchronous blocks that will be then be executed by threads. GCD provides all the thread handling and management. It is close enough to the metal that it is aware of the number of cores on the system and how many are in use and will created threads appropriately. (Multithreading is difficult because the number of threads that an app should optimally use depends on the run time availablity of cores.) Mac OS X 10.6 gains a lot of performance benefits from using GCD built into the operating system. Now that technology has been made open source which has some interesting reprecussions. It means that people have an incentive to port the library to other systems (particularly servers and research machines). It also means that people are more likely to use blocks in the C language which is not part of the standard (but may become standard if it becomes widely used). Also because the Apache license is not copyleft, this technology may appear in unexpected closed source products as well. In any case, this is very interesting tech and nice to see it made open source.

Official site: http://libdispatch.macosforge.org/

Some more reading on blocks and the GCD from Ars Technica (highly recommended):
C Blocks
GCD part 1
GCD part 2

Note: interesting tid bit about blocks is that a similar functionality has been proposed in C++0x as lamda functions. Both C++ and C are in the process of being updated to new standards. C++ has had a lot more work done on it but Apple has a vested interest in advancing C since their primary language Objective-C is a superset of C and would gain no benefit from advances in C++.
Disclaimer: I am no longer active. Any posts, PMs or other communication I use has no guarantee of accuracy or follow up.
Download Eschaton: Mediafire

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: Grand Central Dispatch goes Open Source

Post by nil » Sun Sep 20, 2009 12:21 pm

Thanks for the article Alt. After getting snow leopard, I decided to skim it and play around a little bit with C blocks:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

typedef void (^iterate_t)(int);

void iterate(int from, int to, iterate_t block)
{
    for (; from <= to; from++)
    {
        block(from);
    }
}

int main(int argc, const char *argv[])
{
    iterate_t block = ^(int index)
    {
        printf("Printing index %i\n", index);
    };
    iterate(0, 3, block);
    
    return EXIT_SUCCESS;
}

// output:
/*
Printing index 0
Printing index 1
Printing index 2
Printing index 3
*/
Pretty neat.

Also, this is kind of off topic but Apple has also included a 'new' compiler in snow leopard called Clang and they included its static analyzer, which is great for pointing out problems in C/ObjC code.

[edit]: GCD's dispatch_* closures look really awesome.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Amy
Green Beret
Posts: 3628
Joined: Mon Nov 17, 2008 6:22 pm
Location: Mota-Lev's house.
Contact:

Re: Grand Central Dispatch goes Open Source

Post by Amy » Mon Sep 21, 2009 3:59 pm

i want to get snow leopard. is it alot better than leopard or just a little?
MGM Sig
Mota-Lev wrote:Its like watching an Asian girl crush a cats brain through its eye socket with high heels.. Its horrible but I just can't look away :/.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests