There's a Channel 9 Video that attempts to explain the difference between Threads and Tasks. I usually like the Channel 9 videos for their technical accuracy, but from my understanding, this one gets some of the key statements wrong.
Here are the statements:
- @~1:48 to ~2:30: Threads are expensive in memory and time. Each thread gets 1 MB of user mode stack.
- @~2:30 to 2:37: It also takes time to create threads. Time to allocate the data structures and initialize them.
- @~2:38 to 2:45: Context switching also takes time.
- @~2:58 to 3:23 and 3:48 to : Tasks make use of a multi-core system [...] So when you do compute, you're using multiple cores.
- @~5:02 to 5:30: Threads run on a single core and context switching happens a lot.
- @~5:40 to 5:51: Threads have different ways to execute. They execute on multiple cores.
- @~6:20 to 6:30: As you see here in the diagram, Threading has overhead.
And here are my thoughts that I'd like to be confirmed or disproven:
The amount of call stack is configurable. That way, the thread limit is not ~1300 but up to 12000 for a 32 bit process. Those who have a copy of SysInternals TestLimit available can try it:
D:\>testlimit -t -n 64 Testlimit v5.04 - test Windows limits By Mark Russinovich - www.sysinternals.com Creating threads with 64 KB stacks... Created 12500 threads. Lasterror: 8
The stack size is also exposed in the Thread Constructor for .NET
Tasks rely on Threads as a basis. Those threads are taken from the thread pool, but nevertheless, the threads of the thread pool need to be created before they can be used. AFAIK, Mark Russinovich also explained in the Windows Internals book, that the kernel structure (
_ETHREAD
) is kept in memory for reuse. This minimizes the overhead of allocation and reduces it to initialization.I didn't find the exact place I was looking for, but in Windows Internals 6, part 1 it says on page 417:
[...] the executive thread object might or might not be deallocated.
Since Tasks rely on threads as the technical implementation, context switching occurs in any case.
If I have 2 threads, they can also be executed on different processors. IMHO that's the whole idea of it.
The speaker is taslking about threads on a single core system. IMHO, tasks will have almost no benefit in that case as well. See 4.)
See 4. and 5.)
The slide may be correct, but it does not show the actual reason for that. The slide it missing the ~15 ms time slices which cause the context switching. The overhead can only be reduced using Tasks if the thread needs to wait for a result.
In that case, the lower part of the slide is incorrect as well, because the first part of Work 1 seems to be blocking, in which case Work 2 could be executed only. When Work 2 finishes, the condition to continue with Work 1 might be fulfilled. Only if all that occurs within a time slice, Tasks have a benefit.
In any case, a context switch will occur with Tasks as well, sooner or later.
I have tried confirming my understanding with the help of these questions on SO
Above might seem like 7 individual questions. I asked them all in one place because
- the all have their source in one video
- some questions depend on each other
- it's IMHO more important to find the information in one place than following the SO rule of asking one question at a time.