From the course: Threading in C#

Tasks vs. threads - C# Tutorial

From the course: Threading in C#

Start my 1-month free trial

Tasks vs. threads

- [Instructor] So far we've been using threads in our examples. Threads are great, in fact, threads are the most low-level constructs when it comes to multithreading. However, working with threads could be very challenging. How do we actually return a value from another thread? Can we do that? Let's say I have a thread which is my main thread, and then I spawn off a completely different thread, it does some operations, and I want it to return a sudden value, back to the main thread. The only way we can make that work is by using Join, waiting of that thread to complete the execution, and then use a share field which might have been written to by that particular thread, and then access that shared field to get the value. It works, but it can get complicated. So Tasks come to our rescue. Task is a higher level abstraction and they're capable of returning a value. Tasks are very handy because they are compositional in nature. In fact, you can use continuations and chain any amount of tasks together. Tasks are capable of using the thread pool and one of the best features about Tasks is, they're very handy for I/O bound operations. Now, what's an I/O bound operation? So far what we've seen is operations where the CPU has been used. The resources of the local machine have been used. And these are called computation-bound operations. Let's say you want to create a Fibonacci series, and you want the Fibonacci series to take n as the variable, which is the count. So let's say you want Fibonacci of 40, and n is passed as 40. Well it might take few seconds, whatever language you're doing this in wouldn't really matter, but it's going to use the CPU and the memory, and will actually give you the result in few seconds. Let's say you do a Fibonacci of 2000, and it ends up being five seconds, or maybe Fibonacci of 100,000, it ends up being 10 seconds. Now this is a computation-intensive operation that you're running on your local machine. But there are times when we may want to make and out-of-process call, something like a call to a database, or a call to the API, or even to a web server. Let's say we're making a call to Google.com, we do a search over there, and we wait for Google to give us our results. And we bring those results back, and we use it in our application. I/O bound operations can take any amount of time because we're waiting on something that's external to us. And while we're making an I/O bound call, for the most part, it may not be wise for us to actually starve the resources of our local machine, or the web server where the application has been running. So what we want to do, is make the call and actually release the resources, and keep a call back, which will be called when we get the results back from that call. So for I/O bound operations, Tasks can use, TaskCompletionSource. And that makes our lives so much easier.

Contents