3

I want to learn how to add concurrency to some synchronous program, and take Fibonacci algorithm as example. I wrote these code, but I found that it doesn't have any concurrency at all. All the code running in a single thread until it finish. Could anybody can explain to me why it doesn't reflect the async?

 async static Task<int> Fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { var t1 = Fibonacci(n - 1); var t2 = Fibonacci(n - 2); return (await t1) + (await t2); } } static int Main(string[] args) { var fib = Fibonacci(25); fib.Wait(); Console.WriteLine( fib.Result ); Console.ReadKey(); return 0; } 

Under Michael's prompt, I try create Task in the async function, and it works. But I noticed that an async function returns a Task type value, it is the same as Task.Run(). Both of the tasks will run immediately, but the t1 will not run into a new thread automatically. So could anyone to tell me, what's the different between these two tasks. Can I make the async function run into new thread automatically?

 async static Task<string> Async1() { return DateTime.Now.ToString(); } static void Main(string[] args) { Task<string> t1 = Async1(); Task<string> t2 = Task.Run<string>(() => { return DateTime.Now.ToString(); }); } 
5
  • What do you mean it doesn't react? What happens when you run your program? Commented Apr 20, 2012 at 12:08
  • How do you expect it to react? Commented Apr 20, 2012 at 12:11
  • I hope you do know this is a very inefficient algorithm (hint: work out how often fib(20) is calculated)? OK it's fine for testing async work (takes a long time :-) ). Commented Apr 20, 2012 at 12:12
  • this algorithm has not good order(O(2^N)). for polynomial order you can use dynamic programing or calculate matrix of fibonacci...would you have more help? Commented Apr 20, 2012 at 12:45
  • I just take Fibonacci algorithm as an example. I know maybe it may not suitable for changing to multithread. Commented Apr 22, 2012 at 4:47

4 Answers 4

5

First, you're using an experimental CTP for the async/await model of .NET 4.5. You should expect things to behave awkwardly in such an environment.

Second, are you aware that you're sleeping indefinitely at the end?

Third, your application is essentially synchronous. You make an asynchronous task and then wait for it immediately afterward. The async/await model does not add concurrency, only asynchrony. It simply allows your application to be responsive while doing multiple things; it does not automatically parallelize. This is why your applications is acting as if it's synchronous.

Sign up to request clarification or add additional context in comments.

Comments

2

There are two reasons for this behavior.

First, async methods run synchronously until they must evaluate an await statement. You are recursing before awaiting, so all of the recursion will be finished synchronously before anything is awaited.

Second, if the awaited task is already completed then they await may run synchronously. Your code never creates a task that isn't already completed. It either synchronously wraps and returns 0 or 1 in a task or synchronously adds two tasks that were completed synchronously and returns that result.

You need to introduce a non-synchronous aspect somewhere. For example:

async static Task<int> Fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { // run one of the recursions concurrently var t1 = Task.Factory.StartNew(() => Fibonacci(n - 1)); var t2 = Fibonacci(n - 2); return (await (await t1)) + (await t2); } } 

Comments

0

You are not starting tasks on the thread-pool at all. await does not introduce parallelism by default.

Change it to:

 var t1 = Task.Factory.StartNew(() => Fibonacci(n - 1)).Unwrap(); var t2 = Task.Factory.StartNew(() => Fibonacci(n - 2)).Unwrap(); 

This is not as efficient as it can get, but it will get you started.

Comments

0
 private async Task<int> ComputeFibAsync(int n) { return await Task.Run( async () => { if (n < 2) { return 1; } else { var result = await Task.WhenAll<int>(ComputeFibAsync(n - 1), ComputeFibAsync(n - 2)); return result[0] + result[1]; } } ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.