For i 100 i consolewriteline0 1 name i thread t1 new

Info icon This preview shows pages 643–645. Sign up to view the full content.

View Full Document Right Arrow Icon
for (; i < 100; ++i) { Console.WriteLine("{0}: {1}", name, i); } }; Thread t1 = new Thread(go); Thread t2 = new Thread(go); t1.Start("One"); t2.Start("Two"); go("Main"); } } This example demonstrates that while it may be convenient to think of value type local variables as living on the stack, it’s not always true. Example 16-5 contains an anony- mous method that makes use of the local i variable declared by the containing method ( Main ), so the C# compiler has been obliged to convert that variable into a field inside a generated class, in order to make it possible for that one variable to be used from multiple methods. To summarize: information that really does live on the stack is private to a particular thread. Unfortunately, using local variables doesn’t necessarily guarantee that the state you’re working with is on the stack. Be wary of reference types—no matter where the reference lives, the thing it refers to will not be on the stack, so you need to understand what other code might have a reference to the object you’re using. Be wary of value types whose implementation you do not control—value types are allowed to contain fields of reference types, so you’re not guaranteed to be safe just because you’re using a value type. And be wary of lambdas and anonymous methods—they can move in- formation off the stack and into a place where it’s accessible to multiple threads at once. We’ll see later what to do if you really have to share information across threads. The examples we’ve seen so far create threads explicitly in order to illustrate the oper- ation of multiple threads. But .NET often creates threads automatically without you having created Thread objects. The most obvious example is the thread that the .NET Framework calls your Main method on, but there are others—some of the asynchronous communication mechanisms we saw in Chapter 13 call back into your code on different threads than the one you started work on. We’ll be seeing more examples of this later in the chapter when we examine .NET’s Asynchronous Programming Model. In fact, it’s relatively unusual to create new threads explicitly. If you need concurrent execution and you’re not using some part of the .NET Framework that supplies you with threads when you need them, it’s often better to use the thread pool or the Task Parallel Library, both of which we’ll see later. Threads | 619
Image of page 643

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
One problem with explicit thread creation is in knowing how many to create. Threads are relatively expensive—each one consumes system resources, and there are also fac- tors that can limit the number of threads in a single process. There’s also a cost in switching between threads—the context switch that occurs when the OS scheduler moves a thread from one logical processor to another. If you have many more runnable threads than logical processors, you’ll pay this cost on a very regular basis, and it can start to have a significant effect on throughput. In an ideal world you would have no more threads than logical processors, avoiding any context switch overhead. However,
Image of page 644
Image of page 645
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern