none
Write a ThreadProc in a template class

    Question

  • Hello there,

    I want to create a thread and the thread executes a template class's member function.

    Is it correct to write something like this:

    class CBar
    {
    public:
        CBar()
        {
        }

        void SayBar()
        {
            cout << "Bar" << endl;
        }
    };

    template<typename T>
    class CFoo
    {
    public:
        CFoo() : m_hThrd(NULL)
        {
        }

        void CallBar()
        {
            m_data.SayBar();
        }

        void SetThread()
        {
            m_hThrd = ::CreateThread(NULL, 0, ThreadProc, (LPVOID)this, NULL, 0);
        }

        static DWORD __stdcall ThreadProc(LPVOID lpParam)
        {
            CFoo<T>* pFoo = reinterpret_cast<CFoo<T>*>(lpParam);

            pFoo->ThreadProcMember();

            return 0;
        }

        void ThreadProcMember()
        {
            cout << "Foo" << endl;
        }

    private:
        HANDLE m_hThrd;
        T m_data;
    };


    void main()
    {
        CFoo<CBar> foo;
        foo.SetThread();
        foo.CallBar();
    }

    Thanks in advance.

    Wednesday, January 23, 2013 3:08 AM

Answers

  • On 1/23/2013 1:31 AM, JupiterLee wrote:

    My application crashes at ThreadProc() when it try to dereference the this pointer (not NULL) which is passed from CreateThread. And it never reaches the class's member function which I want my thread to run.

    My guess would be, the object goes out of scope and is destroyed before the thread gets a chance to run. It's your responsibility to ensure that the object stays alive for as long as the thread is running. That usually means the object must be allocated on the heap.


    Igor Tandetnik

    • Marked as answer by JupiterLee Tuesday, January 29, 2013 6:32 AM
    Wednesday, January 23, 2013 1:44 PM

All replies

  • Looking at your code, it looks right at a glance, although I didn't study it.  I don't see why it would behave differently just because you are in a template.

    Does it fail to compile?  Some other problem?

    Wednesday, January 23, 2013 3:37 AM
  • And don't listen to Vegan -- Wrapping a thread in a class is a perfectly fine approach.  Now, technically you never close the thread handle, so it will leak.  And of course there is no guarantee whether foo or bar will print first.
    Wednesday, January 23, 2013 3:39 AM
  • On 1/22/2013 10:39 PM, SimonRev wrote:

    And of course there is no guarantee whether foo or bar will print first.

    In fact, there's no guarantee that "Foo" will print at all. It's possible for main() to exit and terminate the process before the thread gets a chance to run. There really should be a WaitForSingleObject in there somewhere.


    Igor Tandetnik

    Wednesday, January 23, 2013 4:43 AM
  • Looking at your code, it looks right at a glance, although I didn't study it.  I don't see why it would behave differently just because you are in a template.

    Does it fail to compile?  Some other problem?

    Hi SimonRev,

    My application crashes at ThreadProc() when it try to dereference the this pointer (not NULL) which is passed from CreateThread. And it never reaches the class's member function which I want my thread to run.

    That's why I write this sample code to see if everything is cool or not.

    Wednesday, January 23, 2013 6:31 AM
  • On 1/23/2013 1:31 AM, JupiterLee wrote:

    My application crashes at ThreadProc() when it try to dereference the this pointer (not NULL) which is passed from CreateThread. And it never reaches the class's member function which I want my thread to run.

    My guess would be, the object goes out of scope and is destroyed before the thread gets a chance to run. It's your responsibility to ensure that the object stays alive for as long as the thread is running. That usually means the object must be allocated on the heap.


    Igor Tandetnik

    • Marked as answer by JupiterLee Tuesday, January 29, 2013 6:32 AM
    Wednesday, January 23, 2013 1:44 PM