static wrapper to a non static variable

Posted on 2010-01-06
Last Modified: 2012-06-21

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

Question by:bachra04
    LVL 7

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

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now