Multi-core Programming Programming with Windows Threads

Embed Size (px)

Text of Multi-core Programming Programming with Windows Threads

Multi-core Programming

Multi-core ProgrammingProgramming with Windows Threads2Basics of VTune Performance AnalyzerTopicsExplore Win32 Threading API functionsCreate threads Wait for threads to terminateSynchronize shared access between threads3Programming with Windows ThreadsWindows* HANDLE typeEach Windows object is referenced by HANDLE type variablesPointer to kernel objectsThread, process, file, event, mutex, semaphore, etc.Object creation functions return HANDLEObject controlled through its handleDont manipulate objects directly3Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Introduce and describe the HANDLE data type in Windows programming.

DetailsKernel objects must be manipulated by Windows APIs. Programmers cannot and should not attempt to manipulate these objects directly. Such an arrangement is consistent with the ideas of data abstraction.While this method is convenient, it can also lead to some confusion. We shall see later functions that take a HANDLE parameter, but will perform different functions depending upon the type of kernel object that the handle is referring to.

4Programming with Windows ThreadsWindows* Thread CreationHANDLE CreateThread( LPSECURITY_ATTRIBUTES ThreadAttributes, DWORD StackSize, LPTHREAD_START_ROUTINE StartAddress, LPVOID Parameter, DWORD CreationFlags, LPDWORD ThreadId ); // Out4Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Present and explain the prototype of the CreateThread function.

DetailsThis is the function prototype for CreateThread which creates a Windows thread that begins execution of the function provided as the fourth parameter. This function returns a HANDLE that is used to reference the thread that is created.Since every kernel object has security attributes, the first parameter allows the programmer to specify the security attributes of the thread. The security of objects mainly controls which processes are to be granted access to the object and which are to be denied access. A NULL value sets up default security for the object. Well not go into these details in this lecture.The Stacksize parameter allows the user to specify the amount of stack space that should be reserved for the thread. A value of 0 here sets the stack size to the default of one megabyte.The third parameter, StartAddress is global function declared DWORD WINAPI. This is the function on which the thread will begin its execution.The threaded function can accept a single LPVOID parameter. A value for the thread can be specified in the fourth parameter. If the function being threaded accepts more than one parameter, you can encapsulate all the parameters into a single structure, send the structure as the fourth parameter of the CreateThread call, and decompose the structure into its component parts as the first thing done in the threaded version of the function.CreationFlags allow you to start a thread that is suspended. The default (using 0 for this parameter) is to start the thread as soon as it has been created by the system.ThreadId is an out parameter that is guaranteed unique for all threads running, but may be re-used once a particular thread exits.If the CreateThread function fails to create a thread, it will return FALSE and the reason for the failure can be found by calling GetLastError(). **It is ALWAYS recommended that you check error codes from all Windows API calls. For reasons of space, examples within this lecture will not be checking. However, Do as we say, not as we do. **

Background ALTERNATIVES to CreateThread: There are alternative functions to create threads within the Microsoft C library. These are _beginthread and _beginthreadex. The former should be avoided since it doesnt include security attributes or flags and doesnt return a thread ID. _beginthreadex arguments are exactly the same as CreateThread. We dont show these alternatives for consistency. Students should feel free to use them if they have experience. Students should be directed to MSDN for more information.

5Programming with Windows ThreadsLPTHREAD_START_ROUTINECreateThread() expects pointer to global functionReturns DWORDCalling convention WINAPISingle LPVOID (void *) parameter

Thread begins execution of functionDWORD WINAPI MyThreadStart(LPVOID p);5Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Present more details about the function that is threaded via CreateThread.

DetailsIf more than one parameter is needed by the threaded function, set up a structure to hold all of the needed parts and use a pointer to the structure as the single parameter.

6Programming with Windows ThreadsUsing Explicit ThreadsIdentify portions of code to threadEncapsulate code into functionIf code is already a function, a driver function may need to be written to coordinate work of multiple threadsAdd CreateThread call to assign thread(s) to execute function6Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Describe the major code modifications that are needed to use an explicit threading model.

DetailsThe idea of a driver function is to, perhaps, accept a structure of multiple parameters and then call the function that really does the computation. Also, splitting up the tasks among threads could be done in the driver function before calling the routine that does the work.

7Programming with Windows ThreadsDestroying ThreadsFrees OS resourcesClean-up if done with thread before program completesProcess exit does this for you

BOOL CloseHandle(HANDLE hObject);

7Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Present the method to clean up after threads are done.

DetailsThread handles left lying around take up memory space. Continuously creating threads without cleaning up the handles when done, could lead to a memory leak.

Another possibility would be that the maximum number of threads that can be supported within a process will be reached and no more new threads will be able to be created.

Clean up after your threads!! Examples within this talk dont use this since they are so small and the exit of the process will do the resource reclamation automatically.

Background Not required since process exit will perform cleanup.

8Programming with Windows ThreadsExample: Thread Creation#include #include

DWORD WINAPI helloFunc(LPVOID arg ) { printf(Hello Thread\n); return 0; }

main() {HANDLE hThread = CreateThread(NULL, 0, helloFunc, NULL, 0, NULL );}What Happens?8Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Present first example code using Windows threads.

DetailsPoint out the parameters in the CreateThread call and remind students about what each means.

Build Point:Two possible outcomes:Message Hello Thread is printed on screen Nothing printed on screen. This outcome is more likely that previous. Main thread is the process and when the process ends, all threads are cancelled, too. Thus, if the CreateThread call returns before the O/S has had the time to set up the thread and begin execution, the thread will die a premature death when the process ends.9Programming with Windows ThreadsExample ExplainedMain thread is processWhen process goes, all threads goNeed some method of waiting for a thread to finish9Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Explain the execution of the example code on the previous slide.

Background This slide is a good check to make sure all the salient points have been presented. Also, when reviewing the presentation without the benefit of an instructor, this slide gives the student a chance to understand what was going on in the code segments prior to this.

Transition QuoteTo avoid writing applications that spawn threads and then end before any useful work has the chance to begin, we need some mechanism to wait for threads to finish their processing.10Programming with Windows Threads#include #include BOOL threadDone = FALSE ;

DWORD WINAPI helloFunc(LPVOID arg ) { printf(Hello Thread\n); threadDone = TRUE ;return 0; }main() {HANDLE hThread = CreateThread(NULL, 0, helloFunc, NULL, 0, NULL ); while (!threadDone); }Waiting for Windows* ThreadNot a good idea! // wasted cycles!10Multi-core Programming: Programming with Windows* Threads Speakers Notes

Purpose of the Slide Present a method for pausing the master thread from exiting the process before the spawned thread has had the chance to execute.

DetailsWhile the message will eventually be printed out, the main thread is sitting is a spin-wait until the value of threadDone is changed by the created thread. Of course, at this point, the thread has completed all needed computation and if the thread is killed by the process ending rather than by natural causes of the RETURN, we have not lost any work. However, if running on a single processor system (or with HT), the main thread may be spinning its wheels, soaking up thousands or millions of CPU cycles before the OP/S swaps it out and allows the thread to execute. Obviously, this is not a good idea.

Background This slide shows conceptually (and simply) the needed functionality that is in WaitForMultipleObjects or WaitForSingleObject

Wait for one object (thread)DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds ); Calling thread waits (blocks) until Time expiresReturn code used to indicate thisThread exits (handle is signaled)Use INFINITE to wait until thread terminationDoes not use CPU cycles11Programm


View more >