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. calling a class member function in a seperate thread

calling a class member function in a seperate thread

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

    hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan

    C R 2 Replies Last reply
    0
    • S singersinger

      hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan

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

      Pass the address of the instance of the class from which you want to call a member function as an argument of your thread. When you start a thread, you can pass a user defined parameter that will be passed to the function of your thread. There, pass the pointer to your class instance. In your thread function, cast it back to the class pointer and call the member function.


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

      1 Reply Last reply
      0
      • S singersinger

        hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan

        R Offline
        R Offline
        rupert_durans
        wrote on last edited by
        #3

        Hi I have a friend who somehow encountered that and i remembered that he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread. Try to research about worker thread and UI thread, you may find how to use member viriables of a class inside the thread by using struct. Hope i have helped you somehow. :)

        i need to learn more... i want to be like you guys... i'm just a begginer

        C 1 Reply Last reply
        0
        • R rupert_durans

          Hi I have a friend who somehow encountered that and i remembered that he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread. Try to research about worker thread and UI thread, you may find how to use member viriables of a class inside the thread by using struct. Hope i have helped you somehow. :)

          i need to learn more... i want to be like you guys... i'm just a begginer

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

          rupert_durans wrote:

          he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread

          :confused: What do you mean by making a structure of the class ? Why not simply pass the address of the class instance (the this pointer) to the thread ?


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

          R 1 Reply Last reply
          0
          • C Cedric Moonen

            rupert_durans wrote:

            he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread

            :confused: What do you mean by making a structure of the class ? Why not simply pass the address of the class instance (the this pointer) to the thread ?


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

            R Offline
            R Offline
            rupert_durans
            wrote on last edited by
            #5

            yup, i asked him again and you are right, you may or may not create struct as long as it points to the class instance.

            i need to learn more... i want to be like you guys... i'm just a begginer

            C 1 Reply Last reply
            0
            • R rupert_durans

              yup, i asked him again and you are right, you may or may not create struct as long as it points to the class instance.

              i need to learn more... i want to be like you guys... i'm just a begginer

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

              Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?


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

              R 3 Replies Last reply
              0
              • C Cedric Moonen

                Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?


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

                R Offline
                R Offline
                rupert_durans
                wrote on last edited by
                #7

                hehehe, soweee, i'm just a newbie, i just saw it on my friend's code... :) now i know that it can be direct, can you please give me example on how to declare a pointer to the class instance?

                i need to learn more... i want to be like you guys... i'm just a begginer

                C 1 Reply Last reply
                0
                • C Cedric Moonen

                  Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?


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

                  R Offline
                  R Offline
                  rupert_durans
                  wrote on last edited by
                  #8

                  is it like this one for example? CMyFileSpyDlg* _this;

                  i need to learn more... i want to be like you guys... i'm just a begginer

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?


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

                    R Offline
                    R Offline
                    rupert_durans
                    wrote on last edited by
                    #9

                    can you also answer my question above? HOw to pause a spawned process?

                    i need to learn more... i want to be like you guys... i'm just a begginer

                    1 Reply Last reply
                    0
                    • R rupert_durans

                      hehehe, soweee, i'm just a newbie, i just saw it on my friend's code... :) now i know that it can be direct, can you please give me example on how to declare a pointer to the class instance?

                      i need to learn more... i want to be like you guys... i'm just a begginer

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

                      rupert_durans wrote:

                      can you please give me example on how to declare a pointer to the class instance

                      You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:

                      CMyClass Inst;

                      Here, Inst is an instance of CMyClass. The address of this class is obtained with the & operator:

                      CMyClass* pPointer = &Inst;

                      If you pass this address as a parameter to your thread function, you'll be able to access its member:

                      void MyThreadFunc(LPVOID pParam)
                      {
                      CMyClass* pClass = (CMyClass*)pParam;
                      pClass->MemberFun();
                      }

                      You can also start your thread inside a member function of your class, then simply pass the this parameter:

                      void CMyClass:SomeFunction()
                      {
                      _beginthread(MyThreadFun,0,this);
                      }


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

                      R M 2 Replies Last reply
                      0
                      • C Cedric Moonen

                        rupert_durans wrote:

                        can you please give me example on how to declare a pointer to the class instance

                        You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:

                        CMyClass Inst;

                        Here, Inst is an instance of CMyClass. The address of this class is obtained with the & operator:

                        CMyClass* pPointer = &Inst;

                        If you pass this address as a parameter to your thread function, you'll be able to access its member:

                        void MyThreadFunc(LPVOID pParam)
                        {
                        CMyClass* pClass = (CMyClass*)pParam;
                        pClass->MemberFun();
                        }

                        You can also start your thread inside a member function of your class, then simply pass the this parameter:

                        void CMyClass:SomeFunction()
                        {
                        _beginthread(MyThreadFun,0,this);
                        }


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

                        R Offline
                        R Offline
                        rupert_durans
                        wrote on last edited by
                        #11

                        thank you :cool:

                        i need to learn more... i want to be like you guys... i'm just a begginer

                        1 Reply Last reply
                        0
                        • C Cedric Moonen

                          rupert_durans wrote:

                          can you please give me example on how to declare a pointer to the class instance

                          You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:

                          CMyClass Inst;

                          Here, Inst is an instance of CMyClass. The address of this class is obtained with the & operator:

                          CMyClass* pPointer = &Inst;

                          If you pass this address as a parameter to your thread function, you'll be able to access its member:

                          void MyThreadFunc(LPVOID pParam)
                          {
                          CMyClass* pClass = (CMyClass*)pParam;
                          pClass->MemberFun();
                          }

                          You can also start your thread inside a member function of your class, then simply pass the this parameter:

                          void CMyClass:SomeFunction()
                          {
                          _beginthread(MyThreadFun,0,this);
                          }


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

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

                          Cedric Moonen wrote:

                          You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class: CMyClass Inst; Here, Inst is an instance of CMyClass. The address of this class is obtained with the & operator: CMyClass* pPointer = &Inst;

                          Hi Cédric, Isn't "Inst" an instance of the class and "pPointer" a pointer to an instance of the class? ;) Mark

                          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