Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
4.7.1 Thread Signal Delivery Two types of signals –Synchronous: Occur as a direct result of program execution Should be delivered to currently executing.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Win32 Programming Lesson 9: Jobs & Thread Basics.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Introduction to Processes CS Intoduction to Operating Systems.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Win32 Programming Lesson 11: User-mode Thread Sync (aka: How to crash your machine without really trying…)
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Threads. Readings r Silberschatz et al : Chapter 4.
Windows Threading Colin Roby Jaewook Kim.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Introduction to Operating Systems Concepts
Chapter 4 – Thread Concepts
Threads & Multithreading
OPERATING SYSTEM CONCEPT AND PRACTISE
Module 12: I/O Systems I/O hardware Application I/O Interface
CS 6560: Operating Systems Design
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Scheduler activations
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
Chapter 4: Multithreaded Programming
Threads and Locks.
CSCI 315 Operating Systems Design
Operating System Concepts
13: I/O Systems I/O hardwared Application I/O Interface
CSE 451 Autumn 2003 Section 3 October 16.
Processes Hank Levy 1.
Concurrency, Processes and Threads
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Processes Hank Levy 1.
Concurrency, Processes and Threads
CS703 – Advanced Operating Systems
Module 12: I/O Systems I/O hardwared Application I/O Interface
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)

Where are we?  We know everything there is to know about threads in Windows  Not  But there’s only another 2 lectures on this  Introduce a simplifying idea: thread pooling

What is Thread Pooling  Thread pooling allows us to let the OS create and destroy threads for us  Each worker thread gets woken up when there’s a job to do  The OS decides whether to create a new thread or wait for an existing one  Caveat emptor: setting up a thread pool is quite expensive – the wins come on continued reuse

Scenario 1: Call functions asynchronously  Very common problem  Example: a server which spawns a worker thread to serve a particular client  In this case, the function QueueUserWorkItem is quite handy

QueueUserWorkItem  BOOL QueueUserWorkItem( PTHREAD_START_ROUTINE pfnCallback, PVOID pvContext, ULONG dwFlags );  Queues a thread work item and returns immediately  Callback function must have the form: DWORD WINAPI WorkItemFunc(PVOID pvContext); Note the return code is ignored

What do you Notice?  No call to _beginthreadex or CreateThread  The system creates and manages threads for you  Can gain efficiency as threads are re-used, not recreated for each new work item (system spends less time creating and destroying threads)

Beware  Because the system is managing the threads, you need to be very careful if you put your thread to sleep and wait for an asynchronous request (like IO) Set dwFlags to WT_EXECUTEINIOTHREAD Need to help the system decide that a thread may take a long time to complete (so set WT_EXECUTELONGFUNCTION)

Call Functions at Timed Intervals  Create a queue: HANDLE CreateTimerQueue();  Create timers in the queue: BOOL CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE hTimerQueue, WAITORTIMERCALLBACK pfnCallback, PVOID pvContext, DWORD dwDueTime, DWORD dwPeriod, ULONG dwFlags );

Timer work function  Must be of form: VOID WINAPI WaitOrTimerCallback( PVOID pvContext, BOOL fTimerOrWaitFired );  fTimerOrWaitFired is always TRUE WT_EXECUTEINTIMERTHREAD is interesting  Executes in timer thread  Very dangerous, but useful when used correctly

Deleting Timers  Must do this even for one-shot timers BOOL DeleteTimerQueueTimer( HANDLE hTimerQueue, HANDLE hTimer, HANDLE hCompletionEvent ); Blocks until completion if hCompletionEvent is INVALID_HANDLE_VALUE

But…  Can pass NULL to the timer for hCompletionEvent In this case, the Timer is deleted ASAP, but you won’t know when  Finally, can pass an Event Kernel Object Sets event when the timer is actually deleted

Scenario 3: KO Signaled  Lots of applications spawn a thread to wait on completion of a Kernel Object  Wasteful of resources – if this happens a lot better to use a thread pool CPU Intensive operation when you create/destroy threads Still better than a process

API  BOOL RegisterWaitForSingleObject( PHANDLE phNewWaitObject, HANDLE hObject, WAITORTIMERCALLBACK pfnCallback, PVOID pvContext, ULONG dwMilliseconds, ULONG dwFlags );  hObject is the object to wait on  Time is between 0 and INFINITE

Unregistering a Wait  If the desired object gets signaled multiple times, the Wait object will be woken up multiple times Unless of course you set WT_EXECUTEONLYONCE  But, you must still call: BOOL UnregisterWaitEx( HANDLE hWaitHandle, HANDLE hCompletionEvent );

Call Functions on I/O  Common scenario Wish to call a function when an asynchronous I/O event completes You may want to look up “I/O Completion Ports”

Function Format  BOOL BindIoCompletionCallback( HANDLE hDevice, POVERLAPPED_COMPLETION_ROUTINE pf nCallback, ULONG dwFlags );  Callback function of form: VOID WINAPI OverlappedCompletionRoutine( DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, POVERLAPPED pOverlapped );

Fibers  The idea came when MS thought about application developers porting applications from UNIX to Windows  UNIX lacks the same threading functionality that Windows has, and so implements “threading” quite differently  Fibers make port a lot easier… but they are no substitute for native Windows threads

A what?  Every thread contains one or more fibers  As far as the Kernel is concerned, each thread gets preemptively scheduled The thread decides which fiber to execute Only one fiber gets executed at a time

API  First, must convert the existing thread to a Fiber: PVOID ConvertThreadToFiber(PVOID pvParam); This function allocates memory (about 200 bytes) for the fiber's execution context which contains:  A user-defined value that is initialized to the value passed to ConvertThreadToFiber's pvParam argument  The head of a structured exception handling chain  The top and bottom memory addresses of the fiber's stack (When you convert a thread to a fiber, this is also the thread's stack.)  Various CPU registers, including a stack pointer, an instruction pointer, and others

Create Additional Fibers  No point ever converting a thread if you don’t do this! PVOID CreateFiber( DWORD dwStackSize, PFIBER_START_ROUTINE pfnStartAddress, PVOID pvParam );  Function API is: VOID WINAPI FiberFunc(PVOID pvParam);

Executing Fibers  To make the new fiber execute you call: VOID SwitchToFiber(PVOID pvFiberExecution Context); This stores needed information on the current fiber and sends processing to the new one It’s the only way a fiber can get CPU time Destroy using DeleteFiber