MultiCore Programming Chapter 5 Threading APIs Gan Lin




































- Slides: 36

Multi-Core Programming Chapter 5: Threading APIs Gan Lin OS Lab. GNU

December 28, 2021 2 Contents Threading APIs for MS Windows (Win 32/MFC) . NET Framework (Threading Class) POSIX Threads Ch 5. Threading APIs, Gan. Lin

December 28, 2021 3 Threading APIs for MS Win 32/MFC Thread APIs Since the advent of Windows NT, MS has enable app. developers to write multi-threaded software app. By the advances in processors technology, MS supports more and more advanced threading operations in their threading APIs. As the result, their threading APIs have became a powerful tool for writing multi-threaded app. Ch 5. Threading APIs, Gan. Lin

December 28, 2021 4 Threading APIs for MS Win 32/MFC Thread APIs Create Threads HANDLE Create. Thread( LPSECURITY_ATTRIBUTES lp. Thread. Attributes, SIZE_T dw. Stack. Size, LPTHREAD_START_ROUTINE lp. Start. Address, LPVOID lp. Parameter, DWORD dw. Creation. Flags, LPDWORD lp. Thread. Id); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 5 Threading APIs for MS Win 32/MFC Thread APIs Create Threads There are two return value HANDLE Thread ID Signature DWORD of function pointer WINAPI Thread. Func(LPVOID data); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 6 Threading APIs for MS Win 32/MFC Thread APIs Exit Threads VOID Exit. Thread(DWORD dw. Exit. Code); Fig 1. Implicitly calling Exit. Thread() Ch 5. Threading APIs, Gan. Lin

December 28, 2021 7 Threading APIs for MS Win 32/MFC Thread APIs Problem Create. Thread() dose not perform per-thread initialization of C runtime datablocks and variables Solution MS provides other two method _beginthreadex() _endthreadex() Ch 5. Threading APIs, Gan. Lin

December 28, 2021 8 Threading APIs for MS Win 32/MFC Thread APIs Create. Thread() _beginthreadex() Exit. Thread() _endthreadex() Ch 5. Threading APIs, Gan. Lin

December 28, 2021 9 Threading APIs for MS Win 32/MFC Thread APIs Managing Threads DWORD Suspend. Thread(HANDLE h. Thread); DWORD Resume. Thread(HANDLE h. Thread); BOOL Terminate. Thread(HANDLE h. Thread. DWORD dw. Exit. Code); u Developers must be careful when using Suspend. Thread() & Terminate. Thread() Ch 5. Threading APIs, Gan. Lin

December 28, 2021 10 Threading APIs for MS Win 32/MFC Thread APIs Thread Communication using Windows Event HANDLE Create. Event( LPSECURITY_ATTRIBUTES lp. Event. Attributes, BOOL b. Manual. Reset, BOOL b. Initial. State, LPCTSTR lp. Name); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 11 Threading APIs for MS Win 32/MFC Thread APIs Thread Communication using Windows Event BOOL Set. Event(HANDLE h. Event); BOOL Reset. Event(HANDLE h. Event); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 12 Threading APIs for MS Win 32/MFC Thread APIs Thread Communication using Windows Event DWORD Wait. For. Single. Object( HANDLE h. Handle, DWORD dw. Milliseconds); Return value WAIT_OBJECT_0, WAIT_ABANDONED WAIT_TIMEOUT, WAIT_FAILED Ch 5. Threading APIs, Gan. Lin

December 28, 2021 13 Threading APIs for MS Win 32/MFC Thread APIs Thread Communication using Windows Event DWORD Wait. For. Multiple. Objects( DWORD n. Count, const HANDLE *lp. Handles, BOOL b. Wait. ALL, DWORD dw. Milliseconds); Return value WAIT_OBJECT_I, WAIT_ABANDONED_I WAIT_TIMEOUT, WAIT_FAILED Ch 5. Threading APIs, Gan. Lin

December 28, 2021 14 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization MS defines several different types of synchronization object in Win 32 APIs to avoid some nasty conditions that will occur in multithreaded app. Events Semaphores Mutexes Critical Sections Ch 5. Threading APIs, Gan. Lin

December 28, 2021 15 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization HANDLE Create. Semaphore( LPSECURITY_ATTRIBUTES lp. Semaphore. Attributes, LONG l. Initial. Count, LONG l. Maximum. Count, LPCTSTR lp. Name); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 16 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization HANDLE Open. Semaphore( DWORD dw. Desired. Access, BOOL b. Inherit. Handle, LPCTSTR lp. Name); BOOL Release. Semaphore( HANDLE h. Semaphore, LONG l. Release. Count, LPLONG lp. Previous. Count); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 17 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization HANDLE Create. Mutex( LPSECURITY_ATTRIBUTES lp. Mutex. Attributes, BOOL b. Initial. Owner, LPCTSTR lp. Name); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 18 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization HANDLE Open. Mutex( DWORD dw. Desired. Access, BOOL b. Inherit. Handle, LPCTSTR lp. Name); BOOL Release. Semaphore(HANDLE h. Mutex); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 19 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization Both semaphore & mutex are kernel objects. When we acquire them, we need make a call to the kernel, which will incur overhead. To avoid this overhead, we may use CRITICAL_SECTION data structure, which will run in user space. Ch 5. Threading APIs, Gan. Lin

December 28, 2021 20 Threading APIs for MS Win 32/MFC Thread APIs Thread Synchronization Fig 2. Critical Setion API Ch 5. Threading APIs, Gan. Lin

December 28, 2021 21 Threading APIs for MS Win 32/MFC Thread APIs Atomic Operations Interlocked. Increment(); Interlocked. Decrement(); Interlocked. Exchange. Add(); Interlocked. Compare. Exchange(); Interlocked. And(); Interlocked. Or(); Interlocked. Xor(); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 22 Threading APIs for MS Win 32/MFC Thread APIs Thread Pools In an app. , the number of threads may vary greatly. But create threads is expensive. So developers may create number of threads when the app. starts. But it still has problems, such as the what is the optimal number of threads that should be created. And how these threads being scheduled. Ch 5. Threading APIs, Gan. Lin

December 28, 2021 23 Threading APIs for MS Win 32/MFC Thread APIs Thread Pools MS provides a thread pool API BOOL Queue. User. Work. Item( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 24 Threading APIs for MS Win 32/MFC Thread APIs Thread Pools DWORD WINAPI Function(LOVOID parameter); Value of Flags WT_EXECUTEDEFAULT WT_EXECUTEINIOTHREAD WT_EXECUTELONGFUNCTION Ch 5. Threading APIs, Gan. Lin

December 28, 2021 25 Threading APIs for MS Win 32/MFC Thread APIs Thread Priority In Windows, threads have priorities that range from 0(lowest) to 31(highest). Boost thread’s priority can avoid starvation. Set. Process. Priority. Boost(HANDLE h. Proc, BOOL disable); Set. Thread. Priority. Boost(HANDLE h. Thread, BOOL disable); Get. Process. Priority. Boost(HANDLE h. Proc, BOOL disable); Get. Thread. Priority. Boost(HANDLE h. Thread, BOOL disable); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 26 Threading APIs for MS Win 32/MFC Thread APIs Thread Priority BOOL Set. Thread. Priority( HANDLE thread. Handle, int new. Priority); THREAD_PRIORITY_TIME_CRITICAL THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_ABOVE_NORMAL THREAD_PRIORITY_BELOW_NORMAL THREAD_PRIORITY_LOWEST THREAD_PRIORITY_IDLE Ch 5. Threading APIs, Gan. Lin

December 28, 2021 27 Threading APIs for MS Win 32/MFC Thread APIs Thread Affinity Sometimes we specify a thread to run on a certain processor to have a good result. DWORD_PTR Set. Thread. Affinity. Mask( HANDLE thread. Handle, DWORD_PTR thread. Affinity. Mask); Core 0: 0 x 01, Core 1: 0 x 02, Either: 0 x 03. Ch 5. Threading APIs, Gan. Lin

December 28, 2021 28 Threading APIs for MS Win 32/MFC Thread APIs Thread Affinity BOOL Get. Process. Affinity. Mask( HANDLE process. Handle, PDWORD_PTR process. Affinity. Mask, PDWORK_PTR system. Affinity. Mask); BOOL Set. Process. Affinity. Mask( HANDLE process. Handle, PDWORD_PTR process. Affinity. Madk); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 29 Threading APIs for MS Win 32/MFC Thread APIs User-level Threading with Fibers are completely contained in user space, and use cooperative, which is implemented in the app. PVOID Convert. Thread. To. Fiber(PVOID parameter); PVOID Create. Fiber(DWORD fiber. Stack. Size, PFIBER_START_ROUTINE fiber. Proc. PVOID fiber. Proc. Parameters); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 30 Threading APIs for MS Win 32/MFC Thread APIs User-level Threading with Fibers VOID Switch. To. Fiber( PVOID address. Of. Fiber. Enviroment); VOID Delete. Fiber( PVOID address. Of. Fiber. Enviroment); PVOID Get. Current. Fiber(); Ch 5. Threading APIs, Gan. Lin

December 28, 2021 31 Threading APIs for MS Win 32/MFC Thread APIs Compiling and Linking Multi-threaded App. in Windows /MTd /MDd LIBMT. lib LIBCMTD. lib MSVCRTD. lib LIBPMT. lib LIBCPMTD. lib MSVCPRTD. lib Ch 5. Threading APIs, Gan. Lin

December 28, 2021 32 Threading APIs for MS. NET Framework APIs In 2002, MS introduced a new execution environment, called the Common Language Runtime(CLR), which executes programs in the intermediate code. System. Threading Class Ch 5. Threading APIs, Gan. Lin

December 28, 2021 33 Threading APIs for MS. NET Framework APIs Creating Threads Thread Priority and Other Properties Managing Threads Thread Pools Thread Synchronization Ch 5. Threading APIs, Gan. Lin

34 December 28, 2021 POSIX Threads, or Pthreads, is now the standard threading interface for Linux and is also widely used on most UNIX platforms. Ch 5. Threading APIs, Gan. Lin

35 December 28, 2021 POSIX Thread Creating Threads Managing Threads Thread Synchronization Signaling Compilation and Linking Ch 5. Threading APIs, Gan. Lin

36 December 28, 2021 Ch 5. Threading APIs, Gan. Lin