member function as thread?? 
Author Message
 member function as thread??

Hi all,
 Can I execute a member function of class in seperate4
thread using _beginthread.
If not which API should be used to execute that  member
function seperate thread.
  I assume _beginthread is the best function to create
threads.
regards
popsy


Sun, 28 Dec 2003 03:41:26 GMT  
 member function as thread??
Greets,

    _beginthreadex() is a good function to use if you are using the
mutlithreaded CRT libraries in your own application.  If you're not, you can
use CreateThread().  However, the arguments passed to _beginthreadex()
closely mimic that of CreateThread() so you can use either if you would like
to wrap it.  Make sure that your member function for your class is, of
course, a static member function and that you pass the 'this' pointer as
your user-defined data passed on to the thread procedure.

Regards,

Joe


Quote:
> Hi all,
>  Can I execute a member function of class in seperate4
> thread using _beginthread.
> If not which API should be used to execute that  member
> function seperate thread.
>   I assume _beginthread is the best function to create
> threads.
> regards
> popsy



Sun, 28 Dec 2003 03:49:35 GMT  
 member function as thread??
Hi Popsy,

You can do this, but the member function must be a static member function.
Remember, with C++ member functions the 'this' pointer is passed as the
first parameter on the stack. Windows API's are implemented in C for the
most part, and callback functions are prototyped as such. So if you attempt
to use a non-static member function as a threadproc, the parameters on the
stack won't match up. Windows doesn't have a clue about your C++ class or
its structure. C++ static methods do not implicitly place a 'this' pointer
on the stack allowing you to use them as a threadproc.

Ed Dore
Microsoft.



Sun, 28 Dec 2003 07:12:48 GMT  
 member function as thread??
class CSimple
{
public:

        DWORD RunThread(void)
        {
            DWORD dwResult=0;

            if(NULL==(m_hThread=CreateThread(NULL, 0, &_Thread, (void*)this,
CREATE_SUSPENDED, &m_dwThreadID)))
                return GetLastError();

            SetThreadPriority(m_hThread, THREAD_PRIORITY_LOWEST);
            ResumeThread(m_hThread);

            return dwResult;
        }

        static DWORD WINAPI _Thread(void* pv)
        {

            CSimple* pThis=(CSimple*)pv;
            return pThis->Thread();
        }

        DWORD Thread()
        {
            ...
            return 0;
        }

        HANDLE m_hThread;
        DWORD m_dwThreadID;

Quote:
};



Quote:
> Hi all,
>  Can I execute a member function of class in seperate4
> thread using _beginthread.
> If not which API should be used to execute that  member
> function seperate thread.
>   I assume _beginthread is the best function to create
> threads.
> regards
> popsy



Sun, 28 Dec 2003 09:23:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Q: Member function - Worker thread - MFC

2. Member function - Worker thread - MFC

3. executing member function as thread

4. Q: Member function - Worker thread - MFC

5. Member function - Worker thread - MFC

6. Member function as thread?

7. Is there any way to pass a class member function to thread withought declaring static?

8. Another thread as a member function thread

9. Using class member function as a thread function

10. Using class member function as a thread function

11. static function access member variable and member function

12. Visual C++ - Threads and Member Functions

 

 
Powered by phpBB® Forum Software