• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

static wrapper to a non static variable

The code belows runs perfectly and does the job. The only problem now is that I need it for multiple instances of MyClient.
The problem is that m_hLoginTimer and m_bLoginTimerRunning as well as m_hLoginSem are static.

I need them per instance of MyClient. Is there any solution to wrap a non static variable whithin  a static wrapper object ?

DWORD WINAPI MyClient::LoginTimerThread(LPVOID pv) 
   if (!m_hLoginTimer)
      m_hLoginTimer = CreateWaitableTimer(NULL, TRUE, NULL); 
   TcsClientTBW* pClient = (TcsClientTBW*) pv; // now you have sth. similar to 'this' to use 
   // Set timer to go off in 10 seconds. 
   LARGE_INTEGER li = { 0 }; 
   SetWaitableTimer(m_hLoginTimer, &li, 10000, TcsClientTBW::tbwLoginTimerProc, (PVOID)pClient, FALSE); 
   // Wait in an alertable state for the timer to go off. 
   while (pClient->m_bLoginTimerRunning)  
        SleepEx(INFINITE,           // Wait forever. 
               TRUE );             // IMPORTANT!!! The thread must be in an 
                                   // alertable state to process the APC.  
   ::ReleaseSemaphore(m_hLoginSem, 1, NULL); 
   return 0;

Open in new window

1 Solution
Make the members non-static and then pass the instance of MyClient as void pointer to the thread func.

DWORD WINAPI MyClient::LoginTimerThread(LPVOID pv)
   MyClient * pmc = (MyClient*)pv;
   if (!pmc->m_hLoginTimer)

After that use always pmc-> to acess the non-static members.
If you change pv to also include the this pointer

struct ThreadVar
     MyClient* client;
     TcsClientTBW* tcs;

you can remove the static variables

change the lines

ThreadVar* pVar = (ThreadVar*) pv;

if (!pv->client->m_hLoginTimer)
      pv->client->m_hLoginTimer = CreateWaitableTimer(NULL, TRUE, NULL);

TcsClientTBW* pClient = pVar->tcs;

One way...

Also a Map of statics could be used


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now