Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using global variables #6

Open
Vonfry opened this issue Jan 2, 2021 · 1 comment
Open

Avoid using global variables #6

Vonfry opened this issue Jan 2, 2021 · 1 comment

Comments

@Vonfry
Copy link

Vonfry commented Jan 2, 2021

Global variables will be broken in parallel simulation, such as:

vector<KBaseNodeInfo*> messengerNodeInfoList;

@Vonfry
Copy link
Author

Vonfry commented Jan 2, 2021

These are from omnetpp(5.6.2) simulation document.

4.4.3 How to Avoid Global Variables
If possible, avoid using global variables, including static class members. They are prone to
cause several problems. First, they are not reset to their initial values (to zero) when you
rebuild the simulation in Tkenv/Qtenv, or start another run in Cmdenv. This may produce
surprising results. Second, they prevent you from parallelizing the simulation. When using
parallel simulation, each partition of the model runs in a separate process, having their own
copies of global variables. This is usually not what you want.
The solution is to encapsulate the variables into simple modules as private or protected data
members, and expose them via public methods. Other modules can then call these public
methods to get or set the values.
Calling methods of other modules will be discussed in
section 4.12. Examples of such modules are the Blackboard in the Mobility Framework, and

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant