Windows Concurrency Concepts and APIs Creative Commons License

  • Slides: 46
Download presentation
Windows Concurrency Concepts and APIs Creative Commons License - Curt Hill

Windows Concurrency Concepts and APIs Creative Commons License - Curt Hill

Introduction • We have previously seen one or more presentations on concurrency • We

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 Creative Commons License - Curt Hill

POSIX • Portable Operating System Interface • An attempt by IEEE to standardize OS

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 Creative Commons License - Curt Hill

POSIX • At its inception the UNIX world was already in the process of

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 today – Windows has this library Creative Commons License - Curt Hill

Commentary • Although such a threading library exists in Windows we will instead consider

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 Creative Commons License - Curt Hill

 • Process Definitions – An executable program to the OS – Sole possession

• Process Definitions – 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 Creative Commons License - Curt Hill

Process • There a variety of Process APIs that are available • The usual

Process • There a variety of Process APIs that are available • The usual way to start a process from within a program is the Create. Process function • Once created the program may be accessed with Open. Process • When the process is done the preferred way to clean things up is the Exit. Process Creative Commons License - Curt Hill

Our Interest • Today we are not highly interested in Create. Process – Rather

Our Interest • Today we are not highly interested in Create. Process – Rather we are interested in the corresponding Create. Thread • However, in the interests of full disclosure we will look briefly at the Create. Process parameters • This will not be pretty – It has 10 parameters – Some of these are blocks not simple variables – Written in C Creative Commons License - Curt Hill

Preliminaries • Windows was written in C and not C++ • It was originally

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 Creative Commons License - Curt Hill

Parameter Directions • Microsoft has extended C for their own use • They allow

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 Creative Commons License - Curt Hill

Typing • Microsoft uses a lot of typedef commands to name their types •

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 Creative Commons License - Curt Hill

 • BOOL Types – C does not have a bool so this is

• BOOL Types – 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 Creative Commons License - Curt Hill

Create. Process Signature BOOL WINAPI Create. Process( _In_opt_ LPCTSTR lp. Application. Name, _Inout_opt_ LPTSTR

Create. Process Signature BOOL WINAPI Create. Process( _In_opt_ LPCTSTR lp. Application. Name, _Inout_opt_ LPTSTR lp. Command. Line, _In_opt_ LPSECURITY_ATTRIBUTES lp. Process. Attributes, _In_opt_ LPSECURITY_ATTRIBUTES lp. Thread. Attributes, _In_ BOOL b. Inherit. Handles, _In_ DWORD dw. Creation. Flags, _In_opt_ LPVOID lp. Environment, _In_opt_ LPCTSTR lp. Current. Directory, _In_ LPSTARTUPINFO lp. Startup. Info, _Out_ LPPROCESS_INFORMATION lp. Process. Information ); Creative Commons License - Curt Hill

A Few Notes • Only one of the first two is needed – NULL

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 Creative Commons License - Curt Hill

Process Handle and ID • The process ID is a number that we see

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 Close. Handle method to return it – Parameter is the handle – We will see many different handles Creative Commons License - Curt Hill

Notes • WINAPI is an attribute of most of the functions that are called

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 Creative Commons License - Curt Hill

Worried yet? • Many of the APIs are similarly complicated • We do not

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 Creative Commons License - Curt Hill

Processes and Threads • Starting a new process is not often done – Unless

Processes and Threads • Starting a new process is not often done – Unless this is a scripting environment like Power. Shell • 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 Creative Commons License - Curt Hill

Creating a Thread • A process need an. exe – This supplies all of

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 also several other things Creative Commons License - Curt Hill

Create. Thread Signature HANDLE WINAPI Create. Thread( _In_opt_ LPSECURITY_ATTRIBUTES lp. Thread. Attributes, _In_ SIZE_T

Create. Thread Signature HANDLE WINAPI Create. Thread( _In_opt_ LPSECURITY_ATTRIBUTES lp. Thread. Attributes, _In_ SIZE_T dw. Stack. Size, _In_ LPTHREAD_START_ROUTINE lp. Start. Address, _In_opt_ LPVOID lp. Parameter, _In_ DWORD dw. Creation. Flags, _Out_opt_ LPDWORD lp. Thread. Id ); Creative Commons License - Curt Hill

Notes • WINAPI is an attribute of the function body of the thread •

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 Creative Commons License - Curt Hill

The Body • The third parameter is the address of a function – This

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 Create. Thread Creative Commons License - Curt Hill

The Parameter • Windows cannot know what kind of parameter any function will need

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 Creative Commons License - Curt Hill

Creation Flags • Typical flags are integers – In this case a DWORD •

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 Creative Commons License - Curt Hill

Security Attributes • SECURITY_ATTRIBUTES is a struct • It contains a security descriptor and

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 Creative Commons License - Curt Hill

Final parameter • This is the thread ID • It is a pointer to

Final parameter • This is the thread ID • It is a pointer to an integer – If NULL, then it is not set, that is we have no intention of using the ID • Some API calls will want the thread ID and others will want the thread handle – You should probably save both Creative Commons License - Curt Hill

Thread Body • Must be a stand-alone function – Not a method • It

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 Create. Thread – It may be seen using Get. Exit. Code. Thread function Creative Commons License - Curt Hill

Resume. Thread • Another API call • Takes the handle and releases the thread

Resume. Thread • Another API call • Takes the handle and releases the thread to run Creative Commons License - Curt Hill

Example DWORD WINAPI func(LPVOID parm){ Stuff * sp = (Stuff *)parm; … } …

Example DWORD WINAPI func(LPVOID parm){ Stuff * sp = (Stuff *)parm; … } … HANDLE thand=NULL; DWORD id = 0; thand = Create. Thread( NULL, // security attr 512, // stack size func, // thread body stuff, // Parameter CREATE_SUSPENDED, // when to start &id); // optional id if(thand){ Resume. Thread(thand); … Creative Commons License - Curt Hill

Now What? • We may now start threads • How will we use them?

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 Creative Commons License - Curt Hill

GUI Responsiveness • If your GUI program starts a long computation, it is unable

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 Creative Commons License - Curt Hill

Multiple Unrelated Tasks • This is the server problem • A request is received

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 Creative Commons License - Curt Hill

Array Splitting • This is useful when processing is done on a large array

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 Creative Commons License - Curt Hill

Discussion • The threads do not significantly interact, so no synchronization is needed •

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 Creative Commons License - Curt Hill

Algorithm Splitting • Split the algorithm into stages with no interaction between stages •

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 Creative Commons License - Curt Hill

 • Discussion Most graphics processor units (GPU) are good examples of both this

• Discussion Most graphics processor units (GPU) are good examples of both this and array splitting • The algorithm takes a series of textured three-dimensional objects and then renders them in a raster display – This 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 Creative Commons License - Curt Hill

Threading Guidelines • The creation of a thread is a non-trivial OS operation –

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 Creative Commons License - Curt Hill

Communication • It seems likely that when the thread is done that something needs

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 Creative Commons License - Curt Hill

Components • Most GUI components are not thread safe • How then do we

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 Creative Commons License - Curt Hill

Thought • There is often a misconception that different threads always exist in different

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 Creative Commons License - Curt Hill

Memory and Threads Thread 1 Thread 2 Stack 1 Stack 2 Thread 3 Stack

Memory and Threads Thread 1 Thread 2 Stack 1 Stack 2 Thread 3 Stack 3 Creative Commons License - Curt Hill

Discussion • The main form can execute code in the thread class • The

Discussion • The main form can execute code in the thread class • The thread class can execute code in the main form Creative Commons License - Curt Hill

Handles Again • Handles are one of those resources that need to be reclaimed

Handles Again • Handles are one of those resources that need to be reclaimed – You may drive Windows into problems if you repeatedly use handles and do not give them back • You return a handle with: Close. Handle which takes any handle – Does not matter what type of thing Creative Commons License - Curt Hill

Close. Handle Again • Signature: BOOL WINAPI Close. Handle( _In_ HANDLE h. Object );

Close. Handle Again • Signature: BOOL WINAPI Close. Handle( _In_ HANDLE h. Object ); • Any type of handle may be closed • Usually, the end of process recycles handles • Long running programs need to be more careful Creative Commons License - Curt Hill

Get. Current. Thread • A function that returns a handle to the calling thread

Get. Current. Thread • A function that returns a handle to the calling thread – This handle allows the thread to modify thread attributes as if it were thread creator • Signature: HANDLE WINAPI Get. Current. Thread(void); • Handy so that any thread can get a handle to itself Creative Commons License - Curt Hill

Summary • A process represents a running program – A virtual memory address space

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 Creative Commons License - Curt Hill