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. Use of class function in thread

Use of class function in thread

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
6 Posts 4 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.
  • T Offline
    T Offline
    Tomaz Rotovnik
    wrote on last edited by
    #1

    :confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz

    V J 2 Replies Last reply
    0
    • T Tomaz Rotovnik

      :confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      Yes. One your example, declare StartProc as a static public member function. Kuphryn

      T 1 Reply Last reply
      0
      • V valikac

        Yes. One your example, declare StartProc as a static public member function. Kuphryn

        T Offline
        T Offline
        Tomaz Rotovnik
        wrote on last edited by
        #3

        How can I get few parameters into static class function? Should I declare a structure Thread = CreateThread(NULL,0,AudioIn::StartProc,declared-structure,0,&m_ThreadID); or is there a simple way? Thank you Tomaz

        J 1 Reply Last reply
        0
        • T Tomaz Rotovnik

          How can I get few parameters into static class function? Should I declare a structure Thread = CreateThread(NULL,0,AudioIn::StartProc,declared-structure,0,&m_ThreadID); or is there a simple way? Thank you Tomaz

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          You have to declare a structure and pass a pointer to it. There are some common problems regarding who owns this structure. Consider the following:

          void foo()
          {
          data d;
          // fill d;
          CreateThread(...,&d,...)
          }

          This seems OK and will probably work some times until it suddenly stops to do it. The reason is that, when the thread starts executing and access d, it is perfectly normal that foo has already exited, and d is no longer valid. So, you have to dynamically allocate the data:

          void foo()
          {
          data* d=new data;
          // fill d;
          CreateThread(...,d,...)
          }

          Now the problem is who deletes d? The most reasonable choice is to let the thread delete the data:

          MyThread(LPVOID arg)
          {
          data * d=(data *)arg;
          ...
          delete d; // d no longer needed.
          ...
          }

          This is almost perfect: you should take into account the rare case when the thread does not launch (due to insufficient resources, for instance):

          void foo()
          {
          data* d=new data;
          // fill d;
          if(!CreateThread(...,d,...)){
          delete d; // no thread launched, clean up the mess ourselves
          }
          }

          That's it, hope it helps. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          • T Tomaz Rotovnik

            :confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz

            J Offline
            J Offline
            jason99
            wrote on last edited by
            #5

            Hi Tomaz, the problem is that CreateThread() is a c function...and c doesn't know anything about classes or virtual tables... try this: //--------------------------------------------------- void YourClass::AnyFunction() { // declarations DWORD dwId; // create the thread CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartAddress,(LPVOID)this,0,&dwId); } DWORD YourClass::StartAddress(LPVOID lpParam) { // call the thread function ((YourClass*)lpParam)->ThreadFunction(); } void YourClass::ThreadFunction() { ... } //--------------------------------------------------- in the header you should declare the thread start procedure as follows: static DWORD StartAddress(LPVOID lpParam); ..this should do it

            T 1 Reply Last reply
            0
            • J jason99

              Hi Tomaz, the problem is that CreateThread() is a c function...and c doesn't know anything about classes or virtual tables... try this: //--------------------------------------------------- void YourClass::AnyFunction() { // declarations DWORD dwId; // create the thread CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartAddress,(LPVOID)this,0,&dwId); } DWORD YourClass::StartAddress(LPVOID lpParam) { // call the thread function ((YourClass*)lpParam)->ThreadFunction(); } void YourClass::ThreadFunction() { ... } //--------------------------------------------------- in the header you should declare the thread start procedure as follows: static DWORD StartAddress(LPVOID lpParam); ..this should do it

              T Offline
              T Offline
              Tomaz Rotovnik
              wrote on last edited by
              #6

              :laugh:It is working. Thank you. Tomaz

              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