1 / 28

Windows Threading

Explore Windows OS multi-processor computing system and threading paradigms from legacy to modern interfaces, including cooperative and preemptive models. Learn about thread types, synchronization, and advantages/disadvantages of each model.

pdostal
Download Presentation

Windows Threading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows Threading Colin Roby Jaewook Kim

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

  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 • 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); }

  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); }

  17. Termination ExitThread((DWORD) value); return((DWORD) value); WaitForSingleObject(thread, timeOutValue); GetExitCodeThread(thread, &value); CloseHandle(thread);

  18. Threading Model for Multicore System

  19. Additional Slides

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

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

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

  23. Windows Threading

  24. 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.

  25. 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.

  26. Stack Pages HANDLE thread; thread = CreateThread(0, 16*1024, startroutine, arg, 0, &id);

  27. Client Script Callbacks

More Related