Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TLS in a static library

TLS in a static library

Scheduled Pinned Locked Moved C / C++ / MFC
securityquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chintoo723
    wrote on last edited by
    #1

    All the documentation on thread local storage is about using it in a DLL. I want to use it in a static library. Are there any issues?

    C 1 Reply Last reply
    0
    • C Chintoo723

      All the documentation on thread local storage is about using it in a DLL. I want to use it in a static library. Are there any issues?

      C Offline
      C Offline
      Chintoo723
      wrote on last edited by
      #2

      DWORD g_dwThreadIndex = 0; TCHAR *g_szFixedBuf = 0; TCHAR *function() { if (g_dwThreadIndex == 0) { g_dwThreadIndex = TlsAlloc(); } if (g_szFixedBuf == 0) { g_szFixedBuf = new TCHAR[1024]; } // Do some thread specific work and put it in g_szFixedBuf return g_szFixedBuf; } There are obviously two problems: g_szFixedBuf is initialized once per process and not per thread. I can use a an array of pointers, one for each thread. But since the number of threads is unknown, not sure how long the array should be. The second problem is freeing the memory. Any ideas how this can be addressed?

      M 1 Reply Last reply
      0
      • C Chintoo723

        DWORD g_dwThreadIndex = 0; TCHAR *g_szFixedBuf = 0; TCHAR *function() { if (g_dwThreadIndex == 0) { g_dwThreadIndex = TlsAlloc(); } if (g_szFixedBuf == 0) { g_szFixedBuf = new TCHAR[1024]; } // Do some thread specific work and put it in g_szFixedBuf return g_szFixedBuf; } There are obviously two problems: g_szFixedBuf is initialized once per process and not per thread. I can use a an array of pointers, one for each thread. But since the number of threads is unknown, not sure how long the array should be. The second problem is freeing the memory. Any ideas how this can be addressed?

        M Offline
        M Offline
        Magnus Westin
        wrote on last edited by
        #3

        Why not make a Thread class where you put the Tls functionality. And if all your threads is started from that class you shouldnt have any problem destroying the Tls when the Thread object is destroyed. Never do global stuff... it really hurts the design. Magnus

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups