Grand Central Dispatch & Blocks in Apple's C

Michel Fortin michel.fortin at michelf.com
Tue Sep 1 05:33:07 PDT 2009


On 2009-09-01 08:15:39 -0400, Daniel Keep <daniel.keep.lists at gmail.com> said:

> Oh sod blocks; the REAL awesome in that example is Grand Central
> Dispatch.  For those who don't know, it's a parallelism framework that
> lets you farm off tasks to thread pools, with a few neat additions:
> 
> 1) you can express dependencies and
> 2) the thread pools are managed by the OS itself, so it can load-balance.

And they're dead easy to use. Want an asynchronous foreach loop on the 
global system task queue? Here you go:

	dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {
	    results[i] = do_work(data, i);
	});

I wonder how you could do the above in D2. I mean, assuming there's a 
way to declare a C function taking a block argument and that you can 
prevent prevent the garbage collector from deallocating the 
heap-allocated function frame once it's given to 'dispatch_apply', 
"results" would need to be a shared variable (using unnecessary atomic 
operations?), and the heap-allocated function frame would also need to 
be "shared" since it's given to another thread. Can this work?


> Plus, they have OpenCL.  I've seen about a half dozen OpenCL examples
> specifically for non-Mac machines, but none bother to tell you how to
> actually GET an OpenCL implementation, or if they're even available.  *sigh*

OpenCL is Apple's baby, no wonder they're a little ahead in their 
implementation. Just be pacient, I'm sure others will appear soon.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list