# coding simple structures, potential problems (multithreading) and how to solve them

This post contains two parts. In the first part I will explain what I am trying to do. In the second part I will explain what I fear. My questions are about these fears. Are they justified and if yes how to deal with the problem?

Part 1:

I am developing a simple phraser package "structures" to define structures (objects) that one can operate on. The package is to be used like this

Needs["structures"];
beginStructure[];
defineField[f1,f2,f3];
defineMethod[
constructor[arg1_,arg2_]:=(THIS=newObject;THIS@f1=arg1;THIS@f2=THIS@f1+arg2)
];
defineMethod[
instanceMethod[arg1_,arg2_]:=(THIS@f1=arg1;THIS@f2=arg2)
];
...
endStructure[];


I want to avoid this

Needs["structures"];
beginStructure[];
ref = defineField[f1,f2,f3];
defineMethod[ref,
constructor[arg1_,arg2_]:=(THIS=newObject;THIS@f1=arg1;THIS@f2=THIS@f1+arg2)
];
defineMethod[ref,
instanceMethod[arg1_,arg2_]:=(THIS@f1=arg1;THIS@f2=arg2)
];
...
endStructure[ref];


The coding of the data structure should be as automatic as possible, thus the information flow between the define... methods is done in the background. They leave traces of what they do. I want to avoid passing things explicitly around (like with the ref version).

Essentially, what the functions defineMethod and defineFields do is that they operate on the body (the code) that is provided to them and they arrange things in the background. While doing that they leave traces of what they do so that all the methods (defineMethod, defineFields) can work in synergy (have to be called in the right order etc).

Part 2:

What worries me is multithreading (I know nothing about it, I have never used it). I would like to code all this in a safe way. I see a potential problem if the package is used in a parallel fashion, i.e. two instances of the package are run at the same time. Then it might happen that the following is effectively being executed

...
beginStructure[];
...
beginStructyure[];
...
defineMethod[...];
endStructure[];


which is a disaster, since the defineMethod does not know which environment it should operate within, or depends on. (beginStructure should prepare the stage for other calls).

The question:

I would like to know: are my fears justified? Is it possible that such a situation occurs that the package is beingn run at the same time, in the same kernel? If not, why is it not possible? If yes, how to prevent it?

I could have asked: How to make sure that a piece of code is never excecuted in parallel?, but that would be missleading. The environment is everything here.

-
a well-deserved "-1". this is a revised version of the original post that I edited. the initial post was not clear enough. –  zorank Feb 28 '14 at 15:39
I'll put this in a comment because it is far from a definitive answer (I do not fully understand the question). (1) Your code should be generally safe unless you use explicit parallel calls, and explicitly share variables between kernels. (2) It might not be reentrant, in that entering a dialog session and reinitializing could, I guess, wipe out a preexisting environment. (3) if I understand correctly, you are preferring to work with global variables rather than to localize. As a rule that is not a good idea. Whether your case comprises a viable exception I do not know. –  Daniel Lichtblau Feb 28 '14 at 16:19
@Daniel. Super thanks! Global variables are used, but only within the context of the "structures" package. They are stored there, and being read from there. I am developing a scripting environment and the calls have to come in the rigt order etc. These functions just stich up a code in the background. I know nothing of how to controll parallelism in Mathematica. Is there a command that will enforce part of the code to be executed in a single thread? A user might do something stupid (i.e. I can't know what the user fill in the bodies/arguments to the define... functions). –  zorank Feb 28 '14 at 16:24
For the most part parallelism must be made explicit e.g. ParallelMap. Where parallel processing happens under the hood is in relatively low level code e.g. the BLAS used inside numeric LinearSolve, or code deep in some other areas. I do not foresee any of that interfering in any way with what you have in mind-- where that kicks in is well past Mathematica's evaluator and its semantics. –  Daniel Lichtblau Feb 28 '14 at 16:37