120 likes | 135 Views
Designing and Developing Reliable, Scaleable Multithreaded Windows Applications. Chapter 12-Advanced Thread Safe Libraries and Thread Local Storage. OBJECTIVES. Upon completion of this session , you will be able to: Write DllMain functions Explain and use thread local storage
E N D
Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 12-Advanced Thread Safe Libraries and Thread Local Storage
OBJECTIVES • Upon completion of this session, you will be able to: • Write DllMain functions • Explain and use thread local storage • Create and use thread-safe libraries • Use thread-safe and reentrant functions
Contents • 1. Multithreading Persistent State Problem • 2. The DllMain function • 3. Thread Local Storage • 4. An Alternative to TLS • 5. Lab Exercise 4
1. Multithreaded PersistentState Problem • Case study – strtok C Lib function • char*strtok(char*strToken,constchar*strDelimit); • Find next pattern occurrence in strToken • Must be usable by multiple threads • Each using different parameters • Problem: The function must be “thread safe” • Must maintain per-thread state – not global state
The Solution • Combine • A DLL to implement strtok • A DllMain function • Thread local storage – next section • DLL index created when process attaches • Destroyed when it detaches • A new value is created when a thread attaches
2. The DLLMain Function • BOOL WINAPI DllMain( • HINSTANCE hinstDLL, // handle to DLL module • DWORD fdwReason, // reason for calling • function LPVOID lpvReserved // reserved ); • fdwReason values: • DLL_PROCESS_ATTACH • DLL_PROCESS_DETACH • DLL_THREAD_ATTACH • DLL_TRHREAD_DETACH • Allows global and per-thread initialization and resource allocation and deallocation (Ex: Memory and TLS)
3. Thread Local Storage • Manage each thread’s storage independently • Each thread has its own array of pointers • Indexes allocated and deallocated at any time • Maximum of TLS_MINIMUM_AVAILABLE(64) • Any thread can manage thread indexes • The primary (boss) thread is the most logical
TLS within a Process Thread Number 1 2 3 0 1 2 3 4 TLS Index
Using Thread Local Storage (1 of 2) • DWORD TlsAlloc (VOID) • Returns the allocated index 0 • -1 (0xFFFFFFFF) if no index available • Usually called in DllMain: DLL_PROCESS_ATTACH • BOOL TlsFree (DWORD dwIndex) • Usually called in DllMain: DLL_PROCESS_ATTACH
Using Thread Local Storage (2 of 2) • An individual thread can get and set its TLS values • LPVOID TlsGetValue ( • DWORD dwTlsIndex ) • BOOL TlsSetValue ( • DWORD dwTlsIndex, • LPVOID lpsTlsValue )
4. An Alternative to TLS • You can avoid TLS in many cases by: • Keep function state in a function parameter structure • Similar to a handle • Calling program, or the library, initializes the structure • Subsequent calls update • Advantages: • Each thread can maintain multiple independent states • More robust in case of unexpected thread termination • Often simpler to design and write • Example: • Alternative solution to exercise
5. Lab Exercise 4 • Client/Server system shares SendReceiveSKST.dll • Streaming send and receive messages over WinSock • Each message is null terminated • You don’t know how long it is • Read-ahead buffer is preserved from one call to the next • Source files: SendReceiveSKST.c, serverSKST.c, clientSLST.c – Fix defects in SendReceiveSKSTx.c • Build a project for each. Run several clients. • Alternative solution: keep state in a handle-like structure • Each calling thread uses a distinct handle • Ex: SendReceiveSKHA.c, serverSKHA.c