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. _beginthread, can we create a thread in a class? [modified]

_beginthread, can we create a thread in a class? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 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.
  • G Offline
    G Offline
    Gofur Halmurat
    wrote on last edited by
    #1

    Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error: error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } -- modified at 18:18 Friday 9th November, 2007

    M G D 3 Replies Last reply
    0
    • G Gofur Halmurat

      Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error: error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } -- modified at 18:18 Friday 9th November, 2007

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

      Gofur Halmurat wrote:

      Can i use or create "_beginthread" in the class?

      Sure.  But, if your thread proc is a class method, that method needs to be static... class CMaClasse { public: void FoncAppnt(void* args); static void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(&``CMaClasse::``FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      G 1 Reply Last reply
      0
      • M Mark Salsbery

        Gofur Halmurat wrote:

        Can i use or create "_beginthread" in the class?

        Sure.  But, if your thread proc is a class method, that method needs to be static... class CMaClasse { public: void FoncAppnt(void* args); static void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(&``CMaClasse::``FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        G Offline
        G Offline
        Gofur Halmurat
        wrote on last edited by
        #3

        Can I use public variable in static function? because i have some public variables in the class

        M 1 Reply Last reply
        0
        • G Gofur Halmurat

          Can I use public variable in static function? because i have some public variables in the class

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

          A static method can only access static class members. To get around this, typically a pointer to an object of the class is passed to the thread proc.  Then the thread proc can use that pointer to access non static members of the class. Here's an example (my changes are in red)...

          class CMaClasse
          {
             void *pThreadArgs;
          public:
             void FoncAppnt(void* args);
             static void FoncApp(void* arg);
          };

          void CMaClasse::FoncAppnt(void* args)
          {
             pThreadArgs = args;

          _beginthread(&CMaClasse::FoncApp,0,this);
          }

          void CMaClasse::FoncApp(void* arg)
          {
             // The passed param is a CMacClasse pointer
             CMaClasse *pThis = (CMaClasse *)arg;

          // example of accessing a class member
             pThis->pThreadArgs->...

          //...
          }

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          G 1 Reply Last reply
          0
          • M Mark Salsbery

            A static method can only access static class members. To get around this, typically a pointer to an object of the class is passed to the thread proc.  Then the thread proc can use that pointer to access non static members of the class. Here's an example (my changes are in red)...

            class CMaClasse
            {
               void *pThreadArgs;
            public:
               void FoncAppnt(void* args);
               static void FoncApp(void* arg);
            };

            void CMaClasse::FoncAppnt(void* args)
            {
               pThreadArgs = args;

            _beginthread(&CMaClasse::FoncApp,0,this);
            }

            void CMaClasse::FoncApp(void* arg)
            {
               // The passed param is a CMacClasse pointer
               CMaClasse *pThis = (CMaClasse *)arg;

            // example of accessing a class member
               pThis->pThreadArgs->...

            //...
            }

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            G Offline
            G Offline
            Gofur Halmurat
            wrote on last edited by
            #5

            thanks Mark, It helped me alot!!!

            1 Reply Last reply
            0
            • G Gofur Halmurat

              Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error: error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } -- modified at 18:18 Friday 9th November, 2007

              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              Here's a tutorial that may be helpful. I tried to post this earlier but got some goofy error. http://www.computersciencelab.com/MultithreadingTut1.htm[^]

              "We make a living by what we get, we make a life by what we give." --Winston Churchill

              1 Reply Last reply
              0
              • G Gofur Halmurat

                Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error: error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... } -- modified at 18:18 Friday 9th November, 2007

                D Offline
                D Offline
                dtr1
                wrote on last edited by
                #7

                Just to explain this behavior see: http://www.parashift.com/c++-faq-lite/pointers-to-members.html. Briefly, the type of pointer-to-member-function is different from pointer-to-function .There is not cast (new in VC++2005).

                G 1 Reply Last reply
                0
                • D dtr1

                  Just to explain this behavior see: http://www.parashift.com/c++-faq-lite/pointers-to-members.html. Briefly, the type of pointer-to-member-function is different from pointer-to-function .There is not cast (new in VC++2005).

                  G Offline
                  G Offline
                  George L Jackson
                  wrote on last edited by
                  #8

                  george.dumitru wrote:

                  There is not cast (new in VC++2005).

                  I never heard of "not cast"! :confused:

                  "We make a living by what we get, we make a life by what we give." --Winston Churchill

                  D 1 Reply Last reply
                  0
                  • G George L Jackson

                    george.dumitru wrote:

                    There is not cast (new in VC++2005).

                    I never heard of "not cast"! :confused:

                    "We make a living by what we get, we make a life by what we give." --Winston Churchill

                    D Offline
                    D Offline
                    dtr1
                    wrote on last edited by
                    #9

                    There is no cast to non-member function pointer.

                    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