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. Access a pointer value changed in another class

Access a pointer value changed in another class

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
7 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.
  • M Offline
    M Offline
    meerokh
    wrote on last edited by
    #1

    For synchronization purpose, I am implementing a wait function that is gonna wait until a package is executed. I have following code in user application

    bool finished;
    function(){
    int id = createpackage(&finished);// user creates a package(basically another function) for the execution
    wait(&finished)//if i need to wait for the result of above created package
    // do remaining stuff
    }

    Inside the library(i am developing), i am trying to do this package.cpp

    createpackage(bool* finished){
    *finished = false;
    package pack(function, arguments, finished);// this calls the constructor of package

    //package contructor
    package::package(){
    //other memeber variables inside ctor
    m_packagefinished = *finished; // As package is being created so finished = false

    void package::setHandle(){
    lock
    //i am not sure how to set the value of finished to true so that it reflect the change when i access it in wait(). Any ideas??
    unlock
    }

    }

    In another class i am executing the package and want to set the m_finished to true after packge is being executed.

    packageinstance.execute();
    packageinstance.setHandle();//

    Wait function(implemented in a different class) is as follows:

    wait(bool * finished){
    while(*finished == false)
    yeild(); //If the package is not being executed yet, yeild to another package and execute it
    }

    The problem is that how can i get the correct value of finished inside the wait function.

    V C 2 Replies Last reply
    0
    • M meerokh

      For synchronization purpose, I am implementing a wait function that is gonna wait until a package is executed. I have following code in user application

      bool finished;
      function(){
      int id = createpackage(&finished);// user creates a package(basically another function) for the execution
      wait(&finished)//if i need to wait for the result of above created package
      // do remaining stuff
      }

      Inside the library(i am developing), i am trying to do this package.cpp

      createpackage(bool* finished){
      *finished = false;
      package pack(function, arguments, finished);// this calls the constructor of package

      //package contructor
      package::package(){
      //other memeber variables inside ctor
      m_packagefinished = *finished; // As package is being created so finished = false

      void package::setHandle(){
      lock
      //i am not sure how to set the value of finished to true so that it reflect the change when i access it in wait(). Any ideas??
      unlock
      }

      }

      In another class i am executing the package and want to set the m_finished to true after packge is being executed.

      packageinstance.execute();
      packageinstance.setHandle();//

      Wait function(implemented in a different class) is as follows:

      wait(bool * finished){
      while(*finished == false)
      yeild(); //If the package is not being executed yet, yeild to another package and execute it
      }

      The problem is that how can i get the correct value of finished inside the wait function.

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Are you creating a multi-threaded application? Did you read something about [Synchronization | Microsoft Docs](https://docs.microsoft.com/en-us/windows/desktop/sync/synchronization) ?

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        Are you creating a multi-threaded application? Did you read something about [Synchronization | Microsoft Docs](https://docs.microsoft.com/en-us/windows/desktop/sync/synchronization) ?

        M Offline
        M Offline
        meerokh
        wrote on last edited by
        #3

        yeah i am creating a multi threaded application but i am not allowed to use any libraries etc. Its a bare metal implementation so i need to find a way to do it myself. I am using Pthreads just to emulate the physical cores of a system.

        V 1 Reply Last reply
        0
        • M meerokh

          yeah i am creating a multi threaded application but i am not allowed to use any libraries etc. Its a bare metal implementation so i need to find a way to do it myself. I am using Pthreads just to emulate the physical cores of a system.

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          Well, then did you read something like [Synchronization (computer science) - Wikipedia](https://en.wikipedia.org/wiki/Synchronization\_(computer\_science)) ? Didn't it help?

          1 Reply Last reply
          0
          • M meerokh

            For synchronization purpose, I am implementing a wait function that is gonna wait until a package is executed. I have following code in user application

            bool finished;
            function(){
            int id = createpackage(&finished);// user creates a package(basically another function) for the execution
            wait(&finished)//if i need to wait for the result of above created package
            // do remaining stuff
            }

            Inside the library(i am developing), i am trying to do this package.cpp

            createpackage(bool* finished){
            *finished = false;
            package pack(function, arguments, finished);// this calls the constructor of package

            //package contructor
            package::package(){
            //other memeber variables inside ctor
            m_packagefinished = *finished; // As package is being created so finished = false

            void package::setHandle(){
            lock
            //i am not sure how to set the value of finished to true so that it reflect the change when i access it in wait(). Any ideas??
            unlock
            }

            }

            In another class i am executing the package and want to set the m_finished to true after packge is being executed.

            packageinstance.execute();
            packageinstance.setHandle();//

            Wait function(implemented in a different class) is as follows:

            wait(bool * finished){
            while(*finished == false)
            yeild(); //If the package is not being executed yet, yeild to another package and execute it
            }

            The problem is that how can i get the correct value of finished inside the wait function.

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

            In the user application you should declare the finished variable volatile. In the package.cpp you should capture the pointer (or a reference) to the variable. E.g.

            package::package(){
            //other memeber variables inside ctor
            m_pfinished = finished; // capture the pointer not the value

            void package::setHandle(){
            lock
            *m_pfinished = true; // change the original 'finished' content
            unlock
            }

            H 1 Reply Last reply
            0
            • C CPallini

              In the user application you should declare the finished variable volatile. In the package.cpp you should capture the pointer (or a reference) to the variable. E.g.

              package::package(){
              //other memeber variables inside ctor
              m_pfinished = finished; // capture the pointer not the value

              void package::setHandle(){
              lock
              *m_pfinished = true; // change the original 'finished' content
              unlock
              }

              H Offline
              H Offline
              Henry John
              wrote on last edited by
              #6

              Most of the people know it very properly. For that, I have some different thought about it. So this topic is great. For further information about this thread just browsebrother printer support
              and also this thread is basically developed by c++ coding.

              C 1 Reply Last reply
              0
              • H Henry John

                Most of the people know it very properly. For that, I have some different thought about it. So this topic is great. For further information about this thread just browsebrother printer support
                and also this thread is basically developed by c++ coding.

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

                Sorry ?!

                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