1 / 12

Designing and Developing Reliable, Scaleable Multithreaded Windows Applications

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

agoulet
Download Presentation

Designing and Developing Reliable, Scaleable Multithreaded Windows Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 12-Advanced Thread Safe Libraries and Thread Local Storage

  2. 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

  3. Contents • 1. Multithreading Persistent State Problem • 2. The DllMain function • 3. Thread Local Storage • 4. An Alternative to TLS • 5. Lab Exercise 4

  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

  5. 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

  6. 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)

  7. 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

  8. TLS within a Process Thread Number 1 2 3 0 1 2 3 4 TLS Index

  9. 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

  10. 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 )

  11. 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

  12. 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

More Related