Starting a thread which runs a member function

D

daniel

Hello ,

I have the following code :

//...additional includes cut off
class MyThread{

public:
DWORD WINAPI ThreadProc( LPVOID lpParameter );
void run();
};

DWORD WINAPI MyThread::ThreadProc(LPVOID lpParameter){

cout<<"Thread called\n";
return 0;
}

void MyThread::run(){

CreateThread(NULL, 0, MyThread::ThreadProc, NULL, 0, NULL); //<---
LINE
}

void main(void){


MyThread p;
p.run();

}

When i compile this ( with MSVS 2005 ) i get the following compile
error:

LINE: error C3867: 'MyThread::ThreadProc': function call missing
argument list; use '&MyThread::ThreadProc' to create a pointer to
member.

Then I add &MyThread:ThreadProc instead of MyThread::ThreadProc and i
get another compile error:

LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'

Any logic explanation?

Thank-you ,

Daniel
 
A

AnonMail2005

Hello ,

I have the following code :

//...additional includes cut off
class MyThread{

public:
        DWORD WINAPI ThreadProc(  LPVOID lpParameter );
        void run();

};

DWORD WINAPI MyThread::ThreadProc(LPVOID lpParameter){

        cout<<"Thread called\n";
        return 0;

}

void MyThread::run(){

        CreateThread(NULL, 0, MyThread::ThreadProc, NULL, 0, NULL); //<---
LINE

}

void main(void){

        MyThread p;
        p.run();

}

When i compile this ( with MSVS 2005 ) i get the following compile
error:

LINE: error C3867: 'MyThread::ThreadProc': function call missing
argument list; use '&MyThread::ThreadProc' to create a pointer to
member.

Then I add &MyThread:ThreadProc instead of MyThread::ThreadProc and i
get another compile error:

LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'

Any logic explanation?

Thank-you ,

Daniel
It expects a free standing function not a member function. The bad
news is that you will have to provide that function. The good news is
that you can pass anything you want to that function so it's pretty
easy to pass an address of some object that you can use.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top