Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"AsyncFuture<T>" or what? Future<T> obtained in a background thread -- is it a pattern?

I'd like to have some work done in a background thread as simple as creating a Future var for it and then asking later for the calculated value.

In pseudo-C#-code:

AsyncFuture<int> asyncFuture = new AsyncFuture<int>(FuncToCalculateValue);

//do some other work, or draw UI

if(asyncFuture.NoErrorsHappened){
   int realResult = asyncResult.Value;
}

I can implement such type by my own, but my question is: isn't that some kind of a known pattern? Is there maybe a name for it, or maybe even a framework implementation? Probably in .NET 4.0?

And if it is a pattern, what are the pitfalls associated with it?

like image 998
Max Galkin Avatar asked May 22 '09 08:05

Max Galkin


People also ask

Does future create a new thread?

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.


2 Answers

Yes, Futures are part of the Task Parallel Library which will be in .NET 4.0.

In .NET 4.0 Beta 1, it looks like this exists as a Task<TResult>.

like image 163
Jeff Moser Avatar answered Oct 04 '22 23:10

Jeff Moser


Yacoder, I really like Ayende's two Future implementations. There is some good discussion from Jon Skeet in the comments on the cons.

like image 44
Anthony Mastrean Avatar answered Oct 04 '22 22:10

Anthony Mastrean