Implementation of C++0x's "future" syntax

BCS BCS at pathlink.com
Tue Jan 16 10:55:53 PST 2007


Do you have anyplace to host this? If you want I'll let you put it into 
"scrapple" on dsource.

Daniel Keep wrote:
> 
> I was poking around the stuff on C++0x (which I was led to because I was 
> poking around STL (because I was poking around for a good D container 
> library)) when I found out about "future".
> 
> For those that don't know, the idea is to make asynchronous function 
> calls nice and easy.  The syntax on Wikipedia is this:
> 
>  > int function( int parameter ) ;
>  > // Calls the function and immediately returns.
>  > IdThreadType<function> IdThread = future function(parameter) ;
>  > // Waits for the function result.
>  >int ret = wait IdThread ;
> 
> Anyway, I had a similar idea a while back, and figured now would be the 
> time to see if I could do it.  So I did.  Here's the equivalent in D:
> 
>  > int func( int parameter ); // "function"'s a keyword :P
>  > // Calls the function and immediately returns.
>  > FutureResult!(func) result = future(&func, parameter); // "auto" helps
>  > // Waits for the function result.
>  > int ret = result.value;
> 
> (It also works for functions that have no return type: you would use 
> "result.wait" instead.)
> 
> Not too shabby considering that C++0x is set to be released in 2009. 
> That makes us at least two years ahead of the game :)
> 
> There are only two improvements I can think of off the top of my head:
> 
> 1) Investigate whether "future!(func)(args)" is more efficient.
> 2) Use a thread pool (if the standard library ever gets one).
> 
> Enjoy.
> 
>     -- Daniel
> 



More information about the Digitalmars-d-announce mailing list