Creating futures using Apple's GCD

Posted by jer on Stack Overflow See other posts from Stack Overflow or by jer
Published on 2010-06-06T15:28:38Z Indexed on 2010/06/06 15:32 UTC
Read the original article Hit count: 260

Filed under:
|
|
|
|

I'm working on a library which implements the actor model on top of Grand Central Dispatch (specifically the C level API libdispatch). Basically a brief overview of my system is as such:

  • Communication happens between actors using messages
  • Multicast communication only (one actor to many actors)
  • Senders and receivers are decoupled from one another using a blackboard where messages are pushed to.
  • Messages are sent in the default queue asynchronously using dispatch_group_async() once a message gets pushed onto the blackboard.

I'm trying to implement futures in the language right now, so I've created a new type which holds some information:

  • A group of its own
  • The value being 'returned'

However, I have a problem since dispatch_block_t is of type void (^)(void) so it doesn't return anything. So my idea of in my future_new() function of setting up another group which can be used to execute a block returning a result, which I can store in my "value" member in my future_t structure, isn't going to work.

The rest of the futures implementation is very clear, except it all depends on being able to get the value into the future back from the actor, acting on the message.

When using the library, it would greatly reduce its usefulness if I had to ask users (and myself) to be aware when futures were going to be used by other parts of the system—It just isn't practical.

I'm wondering if anyone can think of a way around this?

© Stack Overflow or respective owner

Related posts about c

    Related posts about concurrency