Asynchronous Programming - Intro - Part 1

  • C# supports parallel execution of code through multi-threading
  • Multi-tasking Fundamentals
    •  There are two distinct types of multi-tasking:
      • Process Based
      • Thread Based
  • Process Based
    • A Process is a unit of Isolation on a single machine. Processes are fully isolated from each other;
    • Multiple Processes do have to share access to the processing cores, but do not share virtual memory address space and can run in different security contexts.
    • This is the model that web servers have used in the past to process multiple requests.
  • Thread Based
    • Threads are independently schedulable sets of instructions with a package of nonshared resources.
    • A thread is bounded within a process.
    • A thread cannot migrate from one process to another.
    • All threads within a process share process-wide resources such as heap memory and operating system resources such as file handles and sockets.
    • The queue-based approach is also applicable to multiple threads, as it is in fact a general purpose asynchronous pattern.
    • Due to the heavy sharing of resources, using multiple threads benefits a lot from this approach.
    • Resource sharing also introduces less complexity in coordinating multiple worker threads and handling thread failure.
  • Why Multiple-Theads preferred over Multiple-Processes
    •  Windows processes are relatively heavyweight constructs when compared with threads.
    • This is due to the loading of the Win32 runtime libraries and the associated registry reads (along with a number of cross-process calls to system components for housekeeping).
    • Therefore, by design on Windows, we tend to prefer using multiple threads to create asynchronous processing rather than multiple processes.
  • Thread Scheduling
    • In Windows, the operating system component responsible for mapping thread execution on to cores is called the Thread Scheduler.
    • As we shall see, sometimes threads are waiting for some event to occur before they can perform any work (in .NET this state is known as SleepWaitJoin).
    • Any thread not in the SleepWaitJoin state should be allocated some time on a processing core and, all things being equal, the thread scheduler will round-robin processor time among all of the threads currently running across all of the processes.
    • Each thread is allotted a time slice and, as long as the thread doesn’t enter the SleepWaitJoin state, it will run until the end of its time slice.
    • Things, however, are not often equal.
    • Different processes can run with different priorities (there are six priorities ranging from idle to real time).
    • Within a process a thread also has a priority; there are seven ranging from idle to time critical.
    • The resulting priority a thread runs with is a combination of these two priorities, and this effective priority is critical to thread scheduling.
    • if a higher-priority thread wants to run, then a lower-priority thread is ejected from the processor (preempted) and replaced with the higher-priority thread.
    • Threads of equal priority are, again, scheduled on a round-robin basis, each being allotted a time slice.
    • You may be thinking that lower-priority threads could be starved of processor time. However, in certain conditions, the priority of a thread will be boosted temporarily to try to ensure that it gets a chance to run on the processor. Priority boosting can happen for a number of reasons (e.g., user input). Once a boosted thread has had processor time, its priority gets degraded until it reaches its normal value.
  • Threads and Resources
    •  Although two threads share some resources within a process, they also have resources that are specific to themselves.
    • Thread-Specific Resources
      • The Stack
      • Each thread gets its own stack. This means that local variables and parameters in methods, which are stored on the stack, are never shared between threads. The default stack size is 1MB, so a thread consumes a considerable amount of resource in just its allocated stack.
      • Thread Local Storage
      • All threads of a process share its virtual address space.
      • The local variables of a function are unique to each thread that runs the function. However, the static and global variables are shared by all threads in the process.
      • With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index. 
      • This value is specific to the thread and cannot be accessed by other threads. 
      • TLS slots are limited in number, The constant TLS_MINIMUM_AVAILABLE defines the minimum number of TLS indexes available in each process. which at the time of writing is guaranteed to be at least 64 per process but may be as high as 1,088.
      • Registers
      • A thread has its own copy of the register values. 
      • When a thread is scheduled on a processing core, its copy of the register value is restored on to the core’s registers. This allows the thread to continue processing at the point when it was preempted (its instruction pointer is restored) with the register state identical to when it was last running.
      •  

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...