Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why I can't beging thread from myclass constructor?

Why I can't beging thread from myclass constructor?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NoName II
    wrote on last edited by
    #1

    CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }

    C H 2 Replies Last reply
    0
    • N NoName II

      CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      It has nothing to do with the fact that you start it in your constructor. The problem is that the 3rd argument you pass to beginthreadex is invalid: the function expects to have a pointer to a global function and you pass a pointer to a member function. The difference is that they don't have the same prototype: the member function has an implicit parameter: the this parameter (that allows the function to know to which instance of the class it belongs to). To solve the problem, you can declare the function as static (in that case, the this parameter is not passed). But of course, you won't be able to access members of the class which are non-static (because the function is global to all instances and thus doesn't belong to a specific instance). Another solution is to pass the pointer to a global function and pass the pointer to the instance of your class as the parameter (this). In the function, you'll then need to cast it to your class:

      unsigned __stdcall MyGlobalFunct(void* param)
      {
      CMyClass* pClass = (CMyClass*)param;
      pClass->MyMemFunction();
      return 0;

      }


      Cédric Moonen Software developer
      Charting control [Updated - v1.1]

      N 1 Reply Last reply
      0
      • N NoName II

        CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        What happens if you run this code unsigned __stdcall MyFunct(void* param); ... ... m_thread=(HANDLE)_beginthreadex(0,0,&MyFunct,0,0,0); unsigned __stdcall MyFunct(void* param) { return 0; }


        WhiteSky


        1 Reply Last reply
        0
        • C Cedric Moonen

          It has nothing to do with the fact that you start it in your constructor. The problem is that the 3rd argument you pass to beginthreadex is invalid: the function expects to have a pointer to a global function and you pass a pointer to a member function. The difference is that they don't have the same prototype: the member function has an implicit parameter: the this parameter (that allows the function to know to which instance of the class it belongs to). To solve the problem, you can declare the function as static (in that case, the this parameter is not passed). But of course, you won't be able to access members of the class which are non-static (because the function is global to all instances and thus doesn't belong to a specific instance). Another solution is to pass the pointer to a global function and pass the pointer to the instance of your class as the parameter (this). In the function, you'll then need to cast it to your class:

          unsigned __stdcall MyGlobalFunct(void* param)
          {
          CMyClass* pClass = (CMyClass*)param;
          pClass->MyMemFunction();
          return 0;

          }


          Cédric Moonen Software developer
          Charting control [Updated - v1.1]

          N Offline
          N Offline
          NoName II
          wrote on last edited by
          #4

          thanks...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups