Access a pointer value changed in another class
-
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 = falsevoid 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.
-
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 = falsevoid 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.
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) ?
-
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) ?
-
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.
Well, then did you read something like [Synchronization (computer science) - Wikipedia](https://en.wikipedia.org/wiki/Synchronization\_(computer\_science)) ? Didn't it help?
-
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 = falsevoid 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.
In the user application you should declare the
finished
variablevolatile
. In thepackage.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 valuevoid package::setHandle(){
lock
*m_pfinished = true; // change the original 'finished' content
unlock
} -
In the user application you should declare the
finished
variablevolatile
. In thepackage.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 valuevoid package::setHandle(){
lock
*m_pfinished = true; // change the original 'finished' content
unlock
}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. -
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.