Starting a thread which runs a member function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daniel

    #1

    Starting a thread which runs a member function

    Hello ,

    I have the following code :

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

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

    DWORD WINAPI MyThread::Threa dProc(LPVOID lpParameter){

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

    void MyThread::run() {

    CreateThread(NU LL, 0, MyThread::Threa dProc, 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::Thre adProc': function call missing
    argument list; use '&MyThread::Thr eadProc' to create a pointer to
    member.

    Then I add &MyThread:Threa dProc instead of MyThread::Threa dProc 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
  • AnonMail2005@gmail.com

    #2
    Re: Starting a thread which runs a member function

    On Aug 15, 10:56 am, daniel <daniel.bal...@ gmail.comwrote:
    Hello ,
    >
    I have the following code :
    >
    //...additional includes cut off
    class MyThread{
    >
    public:
            DWORD WINAPI ThreadProc(  LPVOID lpParameter );
            void run();
    >
    };
    >
    DWORD WINAPI MyThread::Threa dProc(LPVOID lpParameter){
    >
            cout<<"Thread called\n";
            return 0;
    >
    }
    >
    void MyThread::run() {
    >
            CreateThread(NU LL, 0, MyThread::Threa dProc, 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::Thre adProc': function call missing
    argument list; use '&MyThread::Thr eadProc' to create a pointer to
    member.
    >
    Then I add &MyThread:Threa dProc instead of MyThread::Threa dProc 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

    Comment

    • Paavo Helde

      #3
      Re: Starting a thread which runs a member function

      daniel <daniel.baluta@ gmail.comkirjut as:
      Hello ,
      >
      I have the following code :
      >
      //...additional includes cut off
      class MyThread{
      >
      public:
      DWORD WINAPI ThreadProc( LPVOID lpParameter );
      void run();
      };
      >
      DWORD WINAPI MyThread::Threa dProc(LPVOID lpParameter){
      >
      cout<<"Thread called\n";
      return 0;
      }
      >
      void MyThread::run() {
      >
      CreateThread(NU LL, 0, MyThread::Threa dProc, 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::Thre adProc': function call missing
      argument list; use '&MyThread::Thr eadProc' to create a pointer to
      member.
      >
      Then I add &MyThread:Threa dProc instead of MyThread::Threa dProc and i
      get another compile error:
      >
      LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
      'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START _ROUTINE'
      See FAQ:



      And consider to use boost::thread instead.

      hth
      Paavo

      Comment

      Working...