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. VC++ Basic Threading - help

VC++ Basic Threading - help

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++questionworkspace
8 Posts 6 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
    GC104
    wrote on last edited by
    #1

    I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:

    int x;
    CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);

    UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
    {
    int t;
    for (int y=0; y<1000000; y++
    {
    }

    }

    When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(

    C S N G 4 Replies Last reply
    0
    • G GC104

      I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:

      int x;
      CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);

      UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
      {
      int t;
      for (int y=0; y<1000000; y++
      {
      }

      }

      When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You cannot pass a instance method to AfxBeginThread. You've to pass a standard function or a class (i.e. static) method. BTW: have a look at this [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • G GC104

        I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:

        int x;
        CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);

        UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
        {
        int t;
        for (int y=0; y<1000000; y++
        {
        }

        }

        When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(

        S Offline
        S Offline
        Selvam R
        wrote on last edited by
        #3

        try with the following change.

        int x;
        CWinThread *pThread = AfxBeginThread (GcThreadFunction, &x);

        Thanks and Regards, Selvam, http://www.wincpp.com

        1 Reply Last reply
        0
        • G GC104

          I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:

          int x;
          CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);

          UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
          {
          int t;
          for (int y=0; y<1000000; y++
          {
          }

          }

          When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(

          N Offline
          N Offline
          Nuri Ismail
          wrote on last edited by
          #4

          GC104 wrote:

          I have read through the various postings and tried to put some code together but it won't compile.

          Instead of reading through the various postings I can suggest reading this[^] useful article about worker threads. [edit] Sorry I didn't see that CPallini have already gave this link :sigh: [\edit] Regards Nuri Ismail

          1 Reply Last reply
          0
          • G GC104

            I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:

            int x;
            CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);

            UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
            {
            int t;
            for (int y=0; y<1000000; y++
            {
            }

            }

            When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(

            G Offline
            G Offline
            GC104
            wrote on last edited by
            #5

            Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:

            static UINT run(LPVOID p);
            void run();
            volatile BOOL running;
            To start a thread, your handler does

            void CMyView::doInvert()
            {
            running = TRUE;
            AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
            // run is refering to member function 'UINT CMyView::run(LPVOID p)'?
            }

            UINT CMyView::run(LPVOID p)
            {
            CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
            //a CMyView pointer already?
            me -> run();
            return 0;
            }

            void CMyView::run() //run is an overloaded function?
            {
            for(int x=y = 0; running && y < image.height; y++)
            for(int x = 0; running && x < image.width; x++)
            changePixel(x, y);
            running = FALSE;
            }

            C R 2 Replies Last reply
            0
            • G GC104

              Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:

              static UINT run(LPVOID p);
              void run();
              volatile BOOL running;
              To start a thread, your handler does

              void CMyView::doInvert()
              {
              running = TRUE;
              AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
              // run is refering to member function 'UINT CMyView::run(LPVOID p)'?
              }

              UINT CMyView::run(LPVOID p)
              {
              CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
              //a CMyView pointer already?
              me -> run();
              return 0;
              }

              void CMyView::run() //run is an overloaded function?
              {
              for(int x=y = 0; running && y < image.height; y++)
              for(int x = 0; running && x < image.width; x++)
              changePixel(x, y);
              running = FALSE;
              }

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

              // why is a 'this' pointer passed as the pParam? Because of what static functions implies: a static function is shared among all instances of a class. This means that within a static function, you cannot access non-static members of the class. In order to be able to do so, you need to identify which instance you want to manipulate. This is the reason why you pass the instance as a parameter to the thread function.In fact, a static function is similar to a global function in this context. //why is 'p' cast from something that already seems to be //a CMyView pointer already? Because it is received as a LPVOID, which doesn't mean anything. So, to be able to call the Run function on the instance, the compiler needs to know that it is a CMyView object (it can't guess it).

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • G GC104

                Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:

                static UINT run(LPVOID p);
                void run();
                volatile BOOL running;
                To start a thread, your handler does

                void CMyView::doInvert()
                {
                running = TRUE;
                AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
                // run is refering to member function 'UINT CMyView::run(LPVOID p)'?
                }

                UINT CMyView::run(LPVOID p)
                {
                CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
                //a CMyView pointer already?
                me -> run();
                return 0;
                }

                void CMyView::run() //run is an overloaded function?
                {
                for(int x=y = 0; running && y < image.height; y++)
                for(int x = 0; running && x < image.width; x++)
                changePixel(x, y);
                running = FALSE;
                }

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Just adding to the previous reply

                GC104 wrote:

                // why is a 'this' pointer passed as the pParam?

                Because inside the worker thread, a CMyView object is being manipulated (in this case). This particular thread function needs a CMyView object be passed to it. Note: However, it would be a lot better if threads can accept any object (or data type) passed as the parameter in general. Because different threads do different things. Some threads won't require anything at all. Therefore, the best approach would be to pass a pointer to "something". And this something is known to the calling code (it sets up the parameter) and the thread also know what exactly is the pointer pointing to and so you cast it to the appropriate type.

                GC104 wrote:

                //why is 'p' cast from something that already seems to be

                p is a pointer to void (remember that LPVOID is nothing but void *). Therefore, you must cast it to an appropriate type (read my note above?) Hope that helps. :)

                It is a crappy thing, but it's life -^ Carlo Pallini

                G 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Just adding to the previous reply

                  GC104 wrote:

                  // why is a 'this' pointer passed as the pParam?

                  Because inside the worker thread, a CMyView object is being manipulated (in this case). This particular thread function needs a CMyView object be passed to it. Note: However, it would be a lot better if threads can accept any object (or data type) passed as the parameter in general. Because different threads do different things. Some threads won't require anything at all. Therefore, the best approach would be to pass a pointer to "something". And this something is known to the calling code (it sets up the parameter) and the thread also know what exactly is the pointer pointing to and so you cast it to the appropriate type.

                  GC104 wrote:

                  //why is 'p' cast from something that already seems to be

                  p is a pointer to void (remember that LPVOID is nothing but void *). Therefore, you must cast it to an appropriate type (read my note above?) Hope that helps. :)

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  G Offline
                  G Offline
                  GC104
                  wrote on last edited by
                  #8

                  Many thanks for help given, I now have a basic 'thread' the compiles and does roughly what I expect :)

                  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