More notes as I read Concurrent Programming On Windows by Joe Duffy. I hope these notes may come in handy for somebody lacking the time to read this 900+ pages monster of a book.
- if you don't know how CPUs work (registers, interrupts, stack, heap etc) you really should learn that to at least some level, it kind of helps
- everybody knows CLR threads are "managed threads" and there's no guarantee they're 1:1 with native threads. Guess what. At least on Windows, they are always 1:1. In fact each managed thread IS a native thread that just executes (JITed) managed code.
- It turns out the SQL Server team considered hosting a custom version of CLR that would use fibers instead of threads (I suppose in SQL Server they expected very many threads) but eventually abandoned the idea and went with regular 1:1 threads
- creating .NET Thread object does not actually do anything but saves the arguments of the constructor in the private fields. Actual thread is not created until you call Start.
- another interesting fact about managed Thread object is how it's used to manage TLS (thread local storage). Both forms of TLS, the static fields decorated with [ThreadStatic] attribute and those allocated with Thread.AllocateNamedDataSlot method are apparently stored as items in a private collection inside the managed Thread object. In case of [ThreadStatic] the compiler subtitutes references to the static field with invocations of a function on the Thread.Current object that returns corresponding item from the collection that represents the TLS. As you see, no magic involved whatsoever!
- When each thread, including the main one, starts, the stack memory is allocated. For .NET programs the default is 1MB per thread. For ASP.NET, it's 256Kb. The Thread class has a constructor that allows you to specify the stack size. This can be useful to reduce memory footprint if creating very many threads or, on the opposite, to allow deeper recursion by increasing the stack size. The default is stored inside the header of the EXE file and can be changed by EDITBIN.EXE, the tool that comes with Visual C++
This concludes Chapter 3 and gets us as far as page 125.
Tags: