Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windows Threading Colin Roby Jaewook Kim.

Similar presentations


Presentation on theme: "Windows Threading Colin Roby Jaewook Kim."— Presentation transcript:

1 Windows Threading Colin Roby Jaewook Kim

2 Multi-Processor Computing System
OS, Process, and Thread Applications Programming paradigms P Microkernel Multi-Processor Computing System Threads Interface Operating System Hardware Process Processor Thread P

3 Legacy Window Threading Model (Co-operative Threading)

4 Co-operative Threading
Used by Old 16-bit Window Platform Thread continue execution until Thread terminates Executes instruction causing wait (e.g., IO) Thread volunteering to stop (invoking yield or sleep)

5 Architecture for Cooperative Threading Model

6 Advantages & Disadvantages

7 Threading Models from Windows NT to 2003

8 Windows NT~2003 OS Preemptive multi-processing operating system
The OS schedules the CPU time The application can be preempted by OS scheduler

9 Windows Thread The unit of execution (in UNIX, Process is the unit)
Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

10 Windows Thread Types Single Threading Multiple Threading
Each process is started with a single thread Multiple Threading A thread can be created by Win32 Pthread or Windows Thread API Hyper Threading Simultaneous multithreading technology on the Pentium 4 microarchitecture by Intel Supported by Windows 2000 or more

11 Windows Threading Models
Win32 Threading Model Win32 Pthread or Windows Thread API COM (Component Object Model) Threading Model Single Threaded Apartments (STA) Multi Threaded Apartments (MTA) Both Threading Model (STA or MTA)

12 STA & MTA COM Object COM Object

13 Thread Synchronization

14 Win32 Threading Example

15 Creating a Thread start_servers( ) { HANDLE thread; DWORD id; int i; for (i=0; i<nr_of_server_threads; i++) thread = CreateThread(0, // security attributes 0, // default # of stack pages allocated (LPTHREAD_START_ROUTINE) server, // start routine (LPVOID)0, // argument 0, // creation flags &id); // thread ID ... } DWORD WINAPI server(void *arg) { while(TRUE) // get and handle request return(0); To create a thread, one calls the CreateThread routine. This skeleton code for a server application creates a number of threads, each to handle client requests. If CreateThread returns successfully, then a new thread has been created that is now executing independently of the caller of CreateThread. The handle for the new thread is returned; its ID is returned via the last (result) argument. The first parameter is a pointer to the security attributes to be associated with the thread; we supply this as 0 throughout the course. The next parameter is the number of stack pages (in bytes) to allocate physical resources for (one megabyte of virtual memory is allocated; the parameter indicates how much of this initially has real memory and stack space supporting it); 0 means to use the default. The third parameter is the address of the first routine that our thread executes; the next parameter is the argument that’s passed to that routine. The next to the last parameter specifies various creation flags; we don’t supply any here. If CreateThread fails, GetLastError can be used to determine the cause of the failure. The definition of server has the rather odd-looking “WINAPI” as part of its signature. This indicates the subroutine-calling convention used for this routine.

16 When is it done? rlogind(int r_in, int r_out, int l_in, int l_out) { HANDLE in_thread, out_thread; two_ints_t in={r_in, l_out}, out={l_in, r_out}; in_thread = CreateThread(0, 0, incoming, &in, 0, &id); out_thread = CreateThread(0, 0, outgoing, &out, 0, &id); WaitForSingleObject(in_thread, INFINITE); CloseHandle(in_thread); WaitForSingleObject(out_thread, INFINITE); CloseHandle(out_thread); } Here’s the Win32 analog of the rlogind example we showed for POSIX threads. The routine WaitForSingleObject acts, at least in this example, much like pthread_join: it causes the caller to wait until either the thread mentioned in its first argument terminates or the period of time given in its second argument (in milliseconds) transpires. However, unlike as is the case with POSIX threads, the programmer has control over the lifetime of the internal object representing the thread. When a thread is created, a thread object is created in the kernel and the creating thread is passed a handle (reference) to it via the value returned by CreateThread (this is implemented in a manner similar to how file descriptors refer to open files in Unix). The thread object maintains a reference count, which is the number of handles that refer to it. Initially this is two: one handle is returned to the creating thread, the other is implicitly held by the new thread itself, and closed when that thread terminates. The kernel object disappears when (and only when) the reference count drops to zero. Thus, in the example of the slide, when in_thread and out_thread terminate, the reference counts in their thread objects drop from two to one. The creating thread returns from WaitForSingleObject; it must explicitly call CloseHandle so that its hold on the thread object is released, dropping the reference count to zero.

17 Termination ExitThread((DWORD) value); return((DWORD) value);
WaitForSingleObject(thread, timeOutValue); GetExitCodeThread(thread, &value); CloseHandle(thread); A thread terminates either by calling ExitThread or by returning from its first procedure. In either case, it supplies a value that can be retrieved via a call (by some other thread) to GetExitCodeThread. One should be careful to distinguish between terminating a thread and terminating a process. With the latter, all the threads in the process are forcibly terminated. So, if any thread in a process calls ExitProcess, the entire process is terminated, along with its threads. Similarly, if a thread returns from main, this also terminates the entire process, since returning from main is equivalent to calling ExitProcess. The only thread that can legally return from main is the one that called it in the first place. All other threads (those that did not call main) certainly do not terminate the entire process when they return from their first procedures, they merely terminate themselves.

18 Threading Model for Multicore System

19

20 Additional Slides

21 Processes and Threads (1)
Basic concepts used for CPU and resource management

22 Processes and Threads (2)
Relationship between jobs, processes, threads, and fibers

23 Job, Process, Thread & Fiber Mgmt. API Calls
Some of Win32 calls for managing processes, threads and fibers

24 Windows Threading

25 One-to-one model One-to-one model
A process in Windows XP is inert; it executes nothing A process simply owns a 4GB address space that contains code and data for an application. In addition, a process owns other resources, such as files, memory allocations, and threads. Every process in Windows XP has a primary thread. Threads in Windows XP are kernel-level threads. Per-thread data structures: Total user/kernel time, kernel stack, thread-scheduling info., Thread-local storage array, thread environment block (TEB), List of objects thread is waiting on, synchronization info. Etc.

26 Fibers vs. Threads Fibers vs. Threads
Fibers are often called “lightweight” threads. They allow an application to schedule its own “threads” of execution. Fibers are invisible to the kernel. They are implemented in user-mode in Kernel32.dll Fibers interface ConvertThreadToFiber() converts a thread to a running fiber. A new fiber can be created using CreateFiber(). The new fiber runs until it exits or until it calls SwitchToFiber(). Fibers provide a functionality of the many-to-many model.

27 Stack Pages HANDLE thread; thread = CreateThread(0, 16*1024, startroutine, arg, 0, &id); The second argument to CreateThread is the stack size, or, more precisely, the amount of stack space to commit. In Win32, each thread is given one megabyte of virtual memory for its stack. By default (i.e., if you specify 0 for the stack size), two pages are actually “committed,” i.e., paging space is reserved. The stack-space argument allows you to specify how many bytes (rounded up to a multiple of the page size) are committed.

28 Client Script Callbacks
Demo: diagnose a performance problem using perf counters We’ll start with an application under load that over allocates and show high allocation rates, % time in GC, and low RPS We’ll refine the app and reevaluate perf data, making incremental improvements that show improvements in key metrics


Download ppt "Windows Threading Colin Roby Jaewook Kim."

Similar presentations


Ads by Google