Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windows Concurrency Concepts and APIs

Similar presentations


Presentation on theme: "Windows Concurrency Concepts and APIs"— Presentation transcript:

1 Windows Concurrency Concepts and APIs
Copyright © 1997 – 2016 Curt Hill

2 Introduction We have previously seen one or more presentations on concurrency We now need to get specific on how to do it This will be using the Application Program Interface calls for Windows We also consider an aside on POSIX Copyright © 1997 – 2016 Curt Hill

3 POSIX Portable Operating System Interface
An attempt by IEEE to standardize OS interfaces Started in 1988 Most UNIX/LINUX OSs are POSIX compliant Even Windows attempts to have POSIX compliant APIs Largely patterned on UNIX since it was not a proprietary OS as most were in that day Copyright © 1997 – 2016 Curt Hill

4 POSIX At its inception the UNIX world was already in the process of fragmenting among various manufacturer’s versions The original documents desired to standardize: Command line interface I/O interface for files, terminals and network It also has a threading library which is of particular importance Windows has this library Copyright © 1997 – 2016 Curt Hill

5 Commentary Although such a threading library exists in Windows we will instead consider the native Windows threading This is a series of APIs that allow us to create and synchronize our threads Copyright © 1997 – 2016 Curt Hill

6 Definitions Process Thread An executable program to the OS
Sole possession of code and variable memory Must communicate through the OS This is a program with .exe extension Thread Independent execution within a process Share code and variables Starts with a function Each process has one or more threads Copyright © 1997 – 2016 Curt Hill

7 Process There are a variety of Process APIs that are available
The usual way to start a process from within a program is the CreateProcess function Once created the program may be accessed with OpenProcess When the process is done the preferred way to clean things up is the ExitProcess Copyright © 1997 – 2016 Curt Hill

8 Our Interest Today we are not highly interested in CreateProcess
Rather we are interested in the corresponding CreateThread However, in the interests of full disclosure we will look briefly at the CreateProcess parameters This will not necessarily be pretty It has 10 parameters Some of these are blocks not simple variables Written in C Copyright © 1997 – 2016 Curt Hill

9 Preliminaries Windows was written in C and not C++
It was originally written for machines with segmented memory There were two types of pointers 16 bit (within the segment) 32 bit (outside of the segment) The latter are referred to as Long pointers These machines are all gone but the terminology has been retained Copyright © 1997 – 2016 Curt Hill

10 Parameter Directions Microsoft has extended C for their own use
They allow an optional specification of the direction Which their compiler can enforce There are: _in_ _inout_ _out_ They also allow opt_ to be appended to indicate optional Copyright © 1997 – 2016 Curt Hill

11 Typing Microsoft uses a lot of typedef commands to name their types
These names are always all upper case They also have a naming convention We will look at some of their names next Copyright © 1997 – 2016 Curt Hill

12 Types BOOL DWORD LPCTSTR LPXXX
C does not have a bool so this is an integer DWORD A 32 bit integer LPCTSTR A Long Pointer at a C (null terminated) string LPXXX A Long Pointer to an XXX struct There are very many of these sorts of structs Copyright © 1997 – 2016 Curt Hill

13 CreateProcess Signature
BOOL WINAPI CreateProcess( _In_opt_ LPCTSTR lpApplicationName, _Inout_opt_ LPTSTR lpCommandLine, _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ BOOL bInheritHandles, _In_ DWORD dwCreationFlags, _In_opt_ LPVOID lpEnvironment, _In_opt_ LPCTSTR lpCurrentDirectory, _In_ LPSTARTUPINFO lpStartupInfo, _Out_ LPPROCESS_INFORMATION lpProcessInformation ); Copyright © 1997 – 2016 Curt Hill

14 A Few Notes Only one of the first two is needed
NULL if not needed Indicates the program either as a program or as part of a command line creation_flags may also set priority Start_up_info may specify a variety of things about the window, environment variables, standard files etc. Once we do this we can access a process handle and a process id Copyright © 1997 – 2016 Curt Hill

15 Process Handle and ID The process ID is a number that we see in Task Manager Among other places A process handle is a LPVOID object We need it if we want to impact the process while it is running Handles are a resource that must be recycled Use the CloseHandle method to return it Parameter is the handle We will see many different handles Copyright © 1997 – 2016 Curt Hill

16 Notes WINAPI is an attribute of most of the functions that are called
Either API calls or functions passed It is a macro that sets the call conventions A HANDLE should be saved to be closed later A HANDLE is actually a void * pointer It is always used internally in Win A NULL (0) handle means a failure to create We do not know if it is a subscript into a table or a pointer into data structure, but we never change it Copyright © 1997 – 2016 Curt Hill

17 Worried yet? Many of the APIs are similarly complicated
We do not care much for this one However, the thread APIs we are interested in Copyright © 1997 – 2016 Curt Hill

18 Processes and Threads Starting a new process is not often done
Unless this is a scripting environment like PowerShell Starting a new thread is much more common This is commonly done: To exploit multiple cores To isolate the GUI from a more intense computation Copyright © 1997 – 2016 Curt Hill

19 Creating a Thread A process need an .exe
This supplies all of its executable code It also gets its own address space in virtual memory A thread needs a function to serve as its code body It also needs some function stack space We will receive back a handle and thread ID There is also several other things Copyright © 1997 – 2016 Curt Hill

20 CreateThread Signature
HANDLE WINAPI CreateThread( _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ SIZE_T dwStackSize, _In_ LPTHREAD_START_ROUTINE lpStartAddress, _In_opt_ LPVOID lpParameter, _In_ DWORD dwCreationFlags, _Out_opt_ LPDWORD lpThreadId ); Copyright © 1997 – 2016 Curt Hill

21 Notes WINAPI is an attribute of the function body of the thread
The returned handle should be saved to be closed later This also gives power to interact with the thread The stack size must be given Zero means a default size This should be able to contain all parameters, function results, return addresses of all functions that the routine calls When in doubt make it big Copyright © 1997 – 2016 Curt Hill

22 The Body The third parameter is the address of a function
This is just the name with no parameter list – the & is not needed It must have the WINAPI attribute This function should take one parameter which is a void * pointer That pointer is the fourth parameter of CreateThread Copyright © 1997 – 2016 Curt Hill

23 The Parameter Windows cannot know what kind of parameter any function will need Therefore it uses that anomaly the void * pointer Recall that any pointer can be cast into a void * and the reverse Typically the pointer refers to a struct/class which contains as many actual parameters as are needed I did not say this was easy Copyright © 1997 – 2016 Curt Hill

24 Creation Flags Typical flags are integers
In this case a DWORD Each bit represents a Boolean This one is quite simple If all zero then the thread is started immediately If four then the thread is created but must be started with a resume thread CREATE_SUSPENDED is the constant There is an additional flag that says the stack size may be enlarged Copyright © 1997 – 2016 Curt Hill

25 Security Attributes SECURITY_ATTRIBUTES is a struct
It contains a security descriptor and whether the security attributes may be inherited A thread could start additional threads A NULL may be passed for this pointer A default security attributes is then assumed This also means no inheritance of this NULL will be our default Copyright © 1997 – 2016 Curt Hill

26 Final parameter This is the thread ID It is a pointer to an integer
If NULL, then it is not set Some API calls will want the thread ID and others will want the thread handle You should probably save both Copyright © 1997 – 2016 Curt Hill

27 Thread Body Must be a stand-alone function
Not a method It must return a DWORD and take as a parameter an LPVOID style pointer This does not limit the kinds of parameters The return value is not returned to the CreateThread It may be seen using GetExitCodeThread function Copyright © 1997 – 2016 Curt Hill

28 ResumeThread Another API call
Takes the handle and releases the thread to run Copyright © 1997 – 2016 Curt Hill

29 Example DWORD WINAPI func(LPVOID parm){ Stuff * sp = (Stuff *)parm; …
} HANDLE thand=NULL; DWORD id = 0; thand = CreateThread( NULL, // security attr 512, // stack size func, // thread body stuff, // Parameter CREATE_SUSPENDED, // when to start &id); // optional id if(thand){ ResumeThread(thand); Copyright © 1997 – 2016 Curt Hill

30 Now What? We may now start threads How will we use them?
Several possibilities Keep the GUI responsive Multiple unrelated tasks Array splitting Algorithm splitting We will consider some of these and the other needed APIs Copyright © 1997 – 2016 Curt Hill

31 GUI Responsiveness If your GUI program starts a long computation it is unable to respond to the user The solution is to put the computation on one or more threads The problem is to notify the GUI when the thread is done Or update the GUI on the progress of the thread Copyright © 1997 – 2016 Curt Hill

32 Multiple Unrelated Tasks
This is the server problem A request is received Instead of the main thread doing the work it spawns a thread It has a similar problem: When has the daughter thread completed? Creating and deleting threads also has overhead Perhaps a thread may be given a new task after it completes the original Copyright © 1997 – 2016 Curt Hill

33 Array Splitting This is useful when processing is done on a large array of one or more dimensions Each computation involves only the array entry or perhaps entries that are nearby Typically the computation for each entry is short Divide the array into chunks and have a separate thread process each chunk Copyright © 1997 – Curt Hill

34 Discussion The threads do not significantly interact so no synchronization is needed The only point of interaction is starting and stopping The separate threads may share the array, since they are looking at different pieces Shared reading is OK shared writing not Example: image processing Copyright © 1997 – Curt Hill

35 Algorithm Splitting Split the algorithm into stages with no interaction between stages The first stage can read from a file or array and then do so much It starts the second stage with the current results and then goes back to do another This works best if the boundary between each stage can have all of its data encapsulated in one data structure Copyright © 1997 – Curt Hill

36 Discussion Most graphics processor units (GPU) are good examples of both this and array splitting The algorithm to take a series of textured three dimensional objects and render this in a raster display is sufficiently complicated that it may be broken into stages Multi-cores can then work on it in an assembly line sort of process Instruction pipelining is another example Copyright © 1997 – Curt Hill

37 Threading Guidelines The creation of a thread is a non-trivial OS operation Do not make a thread for a task that is very short The overhead of multiple threads puts additional work on the OS A sequence of a few threads is better than a very large number of threads Make an estimate of work to wait time and make this reasonable with the number of cores A thread may receive new work Copyright © 1997 – Curt Hill

38 Communication It seems likely that when the thread is done that something needs to be communicated back to the main form Perhaps during execution as well Create a public method in the main form This is called by the thread Frequently this is only called from outside the class Copyright © 1997 – Curt Hill

39 Components Most GUI components are not thread safe
How then do we safely communicate? Typically need a critical section or some other synchronization operation This is the notion of handshaking Covered in subsequent presentation Copyright © 1997 – Curt Hill

40 Thought There is often a misconception that different threads always exist in different pieces of code memory In practice multiple threads can be in the same code memory at the same time They maintain different stack spaces Consider the next screen Copyright © 1997 – Curt Hill

41 Memory and Threads Thread 1 Thread 2 Stack 1 Stack 2 Thread 3 Stack 3
Copyright © 1997 – Curt Hill

42 Discussion The main form can execute code in the thread class
The thread class can execute code in the main form Copyright © 1997 – Curt Hill

43 Summary A process represents a running program
A virtual memory address space An executable from a file At least one thread A thread represents one execution A process with multiple threads may occupy multiple cores We are now able to create a thread There is still more to know that must wait for the next presentations Copyright © 1997 – 2016 Curt Hill


Download ppt "Windows Concurrency Concepts and APIs"

Similar presentations


Ads by Google