Click here to Skip to main content
15,891,597 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Seven : What Happended To ShellExecute ? [modified] Pin
Nicolas MEURET12-Mar-10 4:38
Nicolas MEURET12-Mar-10 4:38 
GeneralRe: Seven : What Happended To ShellExecute ? Pin
sashoalm12-Mar-10 5:27
sashoalm12-Mar-10 5:27 
QuestionHaving different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 1:20
Code-o-mat12-Mar-10 1:20 
AnswerRe: Having different value for a global variable per thread. Pin
sunlin712-Mar-10 1:37
sunlin712-Mar-10 1:37 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 1:44
Code-o-mat12-Mar-10 1:44 
AnswerRe: Having different value for a global variable per thread. Pin
Jonathan Davies12-Mar-10 1:38
Jonathan Davies12-Mar-10 1:38 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 1:44
Code-o-mat12-Mar-10 1:44 
AnswerRe: Having different value for a global variable per thread. Pin
Chris Losinger12-Mar-10 3:25
professionalChris Losinger12-Mar-10 3:25 
i've had good luck with this class:

/*---------------------------------------------------------*/
/*
** ThreadLocal.h - Thread Local Storage 

  Written by Purush Rudrakshala,
*/

#if !defined (__THREADLOCAL_H__)
#define __THREADLOCAL_H__

#include < list >

class TlsException
{
public:
   explicit TlsException (DWORD error) : m_error (error) {}

private:
   DWORD m_error;                        

   // not implemented
   TlsException ();
};

template < class T > class CISThreadLocal
{
public:
   explicit CISThreadLocal () : m_index (::TlsAlloc ())
   {
      if (m_index == ~0)
      {
#ifdef _DEBUG
         throw TlsException (GetLastError ());
#endif
      }

      // Initialize the critical section.
      InitializeCriticalSection(&g_CritSection); 
   } // cons

   ~CISThreadLocal ()
   {
      if (m_index != ~0)  
      {
         ::TlsFree (m_index);
      }

      m_index = 0;

      // make sure no-one is playing with this data before we yank it away      
      EnterCriticalSection(&g_CritSection); 

      // free thread local objects allocated for all threads
      for (std::list< T *>::iterator it = m_tsdList.begin(); it!=m_tsdList.end(); it++)
      {
          delete (*it);
      }

      m_tsdList.clear();

      // done
      LeaveCriticalSection(&g_CritSection);

      // Delete the critical section.
      DeleteCriticalSection(&g_CritSection);
   } // ~

   inline T* operator -> ()
   {
      return GetThreadLocal ();
   } // ->

   T* GetThreadLocal ()
   {
      T* tsd = 0;

      try
      {
         if (m_index != ~0) 
         {
            tsd = reinterpret_cast <T*> (::TlsGetValue (m_index));
            if (tsd == 0) 
            {
               // thread local storage has not been allocated 
               // for this thread yet, allocate an object and keep
               // it in the list
               tsd = new T;
               BOOL success = ::TlsSetValue (m_index, tsd);
               if (!success)
                  throw TlsException (::GetLastError ());

               EnterCriticalSection(&g_CritSection); 

               m_tsdList.push_back(tsd);
 
               // done
               LeaveCriticalSection(&g_CritSection);
            }
         }
      }
      catch (...)
      {
         tsd = NULL;
      }

      return tsd;
   } // GetThreadLocal

   ///////////////////////////////////////
   // have we allocated anything for this thread?

   bool HasTLS()
   {
      T* tsd = 0;

      try
      {
         if (m_index != ~0) 
         {
            tsd = reinterpret_cast <T*> (::TlsGetValue (m_index));
         }
      }
      catch (...)
      {
         tsd = NULL;
      }

      return (tsd != 0);
   }

   //////////////////////////////////////
   // remove this TLS from the list
   void DeleteThreadTLS()
   {
      try
      {
         if (m_index != ~0) 
         {
            T* tsd = reinterpret_cast <T*> (::TlsGetValue (m_index));

            if (tsd)
            {
               // ok this thread has TLS - kill it
               BOOL success = ::TlsSetValue (m_index, 0);
               if (!success)
                  throw TlsException (::GetLastError ());

               // make sure no-one is playing with this data before we yank it away      
               EnterCriticalSection(&g_CritSection); 

               m_tsdList.remove(tsd);

               delete tsd;

               // done
               LeaveCriticalSection(&g_CritSection);
            }
         }
      }
      catch (...)
      {
      }
   }

private:
   DWORD m_index;  // key to thread specific storage

   // Win32 does not provide a simple way to cleanup
   // resources allocated by a thread when a thread dies
   // So, keep track of thread local objects allocated by
   // various threads in a simple list and destroy
   // when the application exits
   std::list<T *> m_tsdList;

   CRITICAL_SECTION g_CritSection;

   // not implemented
   CISThreadLocal (const CISThreadLocal&);
   void operator = (const CISThreadLocal&);
};


the way it works is:

1. define a class that will hold all your thread-safe data:
class CMyThreadSafeData
{
 ...
int m_foo;
....
};


2. declare and define a single global variable of type CISThreadLocal:

// in a header
extern CISThreadLocal < CMyThreadSafeData > threadSafeData;

...

// in a cpp
CISThreadLocal < CMyThreadSafeData > threadSafeData;


3. whenever you need to access that thread-safe data, do this:

int threadFoo = threadSafeData->m_foo;
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 3:30
Code-o-mat12-Mar-10 3:30 
AnswerRe: Having different value for a global variable per thread. Pin
Russell'12-Mar-10 8:48
Russell'12-Mar-10 8:48 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 10:12
Code-o-mat12-Mar-10 10:12 
QuestionHow to find source of buffer overflow Pin
WernerP11-Mar-10 23:44
WernerP11-Mar-10 23:44 
AnswerRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 0:08
Eugen Podsypalnikov12-Mar-10 0:08 
AnswerRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 0:24
Eugen Podsypalnikov12-Mar-10 0:24 
GeneralRe: How to find source of buffer overflow Pin
WernerP12-Mar-10 2:19
WernerP12-Mar-10 2:19 
GeneralRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 2:44
Eugen Podsypalnikov12-Mar-10 2:44 
AnswerRe: How to find source of buffer overflow Pin
CPallini12-Mar-10 0:35
mveCPallini12-Mar-10 0:35 
AnswerRe: How to find source of buffer overflow Pin
Adam Roderick J12-Mar-10 0:53
Adam Roderick J12-Mar-10 0:53 
GeneralRe: How to find source of buffer overflow Pin
WernerP12-Mar-10 2:21
WernerP12-Mar-10 2:21 
Questiongiving url to .ini file Pin
jannathali11-Mar-10 23:25
jannathali11-Mar-10 23:25 
AnswerRe: giving url to .ini file Pin
KarstenK12-Mar-10 0:08
mveKarstenK12-Mar-10 0:08 
GeneralRe: giving url to .ini file Pin
jannathali12-Mar-10 1:07
jannathali12-Mar-10 1:07 
AnswerRe: giving url to .ini file Pin
Eugen Podsypalnikov12-Mar-10 2:02
Eugen Podsypalnikov12-Mar-10 2:02 
AnswerRe: giving url to .ini file Pin
David Crow12-Mar-10 3:10
David Crow12-Mar-10 3:10 
QuestionCreate a gui for linux. Pin
krish_kumar11-Mar-10 21:09
krish_kumar11-Mar-10 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.