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. passing function pointer

passing function pointer

Scheduled Pinned Locked Moved C / C++ / MFC
5 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.
  • S Offline
    S Offline
    ScotDolan
    wrote on last edited by
    #1

    Right now, We have a ClxThreadClass that provides the capablity to create single worker thread for the class that inherits it. The function below shows the class member function that creates and starts the new thread. The desire worker code reside in _ThreadFunc. The Current working design /*************************************************/ bool ClxThreadClass::CreateNewThread() { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0,_ThreadFunc, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; } However, We sometimes would like to use different function other than _ThreadFunc. How can we overload CreateNewThread member function to take an external and pass to beginthreadex. This external obect is not a member function of base or inherited class. bool ClxThreadClass::CreateNewThread( void *ptr ) { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0, ptr, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }

    Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

    M 1 Reply Last reply
    0
    • S ScotDolan

      Right now, We have a ClxThreadClass that provides the capablity to create single worker thread for the class that inherits it. The function below shows the class member function that creates and starts the new thread. The desire worker code reside in _ThreadFunc. The Current working design /*************************************************/ bool ClxThreadClass::CreateNewThread() { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0,_ThreadFunc, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; } However, We sometimes would like to use different function other than _ThreadFunc. How can we overload CreateNewThread member function to take an external and pass to beginthreadex. This external obect is not a member function of base or inherited class. bool ClxThreadClass::CreateNewThread( void *ptr ) { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0, ptr, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }

      Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Maybe something like this:

      typedef unsigned (__stdcall *PTHREADFUNC)(void *);
      ...
      bool ClxThreadClass::CreateNewThread(PTHREADFUNC pThreadFunc)
      {
      if( m_hThread == NULL )
      {
      // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID)
      m_hThread = (HANDLE)_beginthreadex(NULL, 0, pThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);

      ...

      }

      "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

      T 1 Reply Last reply
      0
      • M Mark Salsbery

        Maybe something like this:

        typedef unsigned (__stdcall *PTHREADFUNC)(void *);
        ...
        bool ClxThreadClass::CreateNewThread(PTHREADFUNC pThreadFunc)
        {
        if( m_hThread == NULL )
        {
        // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID)
        m_hThread = (HANDLE)_beginthreadex(NULL, 0, pThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);

        ...

        }

        "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        typedef unsigned (__stdcall *PTHREADFUNC)(void *);

        if this function points to global function, it will work .. if it point to class member function.. then there would be some trouble.. i.e. we have to change the Funtion ptr declaration and pass the pointer of the class to that function!

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

        M 1 Reply Last reply
        0
        • T ThatsAlok

          Mark Salsbery wrote:

          typedef unsigned (__stdcall *PTHREADFUNC)(void *);

          if this function points to global function, it will work .. if it point to class member function.. then there would be some trouble.. i.e. we have to change the Funtion ptr declaration and pass the pointer of the class to that function!

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Right. The OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me. Mark

          "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

          T 1 Reply Last reply
          0
          • M Mark Salsbery

            Right. The OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me. Mark

            "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            Mark Salsbery wrote:

            he OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me.

            oops my mistake.. it seems work is taking over me [:)]

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

            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