Threading from inside your class?

This is a discussion on Threading from inside your class? within the Windows Programming forums, part of the Platform Specific Boards category; Hey, quick question for y'all. I can't seem to figure out how to create a thread within a class using ...

  1. #1
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    Threading from inside your class?

    Hey, quick question for y'all. I can't seem to figure out how to create a thread within a class using a function in that class.

    ie: if my class TestIt has two functions: One private called collectdata, one public called collect. In my program I want collect() to call _beginthread (or createthread or whatever will work) with collectdata as the function. Is it even possible?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you can pass a pointer to the class into beginthread() with a static or global thread function. I prefer static on the class you're passing in and then immediately calling a method that isn't static.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Care to elaborate? maybe with some example code?

    Thanks.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    Code:
    #include <windows.h>
    
    #include <iostream>
    using namespace std;
    
    
    class FooBar
    {
        HANDLE m_hthread;
        DWORD m_threadid;
    
        // static method for thread entry point
        static DWORD WINAPI ThreadProc(LPVOID lpv)
        {
            return reinterpret_cast<FooBar*>(lpv)->ThreadMethod();
        }//ThreadProc
    
        // member function called by static thread entry point
        DWORD ThreadMethod()
        {
            for (int n = 0; n < 5; n++)
            {
                cout << "Look at me! I'm a thread!!" << endl << flush;
                ::Sleep(1000);
            }//for
    
            return 0;
        }//ThreadMethod
    public:
        FooBar() : m_hthread(NULL), m_threadid(0) {}
        ~FooBar() 
        {
            // stop thread here if need be
            if (m_hthread != NULL)
            {
                ::CloseHandle(m_hthread);
            }//if
        }//destructor
    
        // start the thread running
        bool StartThread()
        {
            m_hthread = ::CreateThread(NULL, 0, FooBar::ThreadProc, 
                                       reinterpret_cast<void*>(this), 
                                       0, &m_threadid);
            return m_hthread != NULL;
        }//StartThread
    
        // wait for the thread to exit
        bool Join()
        {
            if (m_hthread == NULL)
                return false;
            return ::WaitForSingleObject(m_hthread, INFINITE) == WAIT_OBJECT_0;
        }//Join
    };//FooBar
    
    int main()
    {
        FooBar fb;
    
        fb.StartThread();
    
        // wait for the thread to exit
        fb.Join();
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-02-2006, 09:32 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 12:46 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21