Thread
Handling
See Also: ucNewThread, ucReleaseItem
Apartment-model threads
uCalc FMP allows you to group definitions and evaluations into independent threads. Whatever you define or evaluate in one thread group is invisible to other thread groups. To allocate a new thread group, use ucNewThread. This will return a handle that you can pass as the last argument to functions such as ucParse, ucDefine, ucDefineFunction, ucDefineVariable, etc...
Classes and Objects
You can re-organize functions in the include file into a class. The details will vary from compiler to compiler. But generally, when the class is constructed, you should call ucNewThread to obtain a handle for the object instance. ucParse, ucDefine, etc... should each be defined as members of the class, and you should use the thread handle with each of these functions. When the class instance is destroyed, you should call ucReleaseItem with the handle of the thread.
Parent threads
Whenever you define a new thread, it will generally be associated with a parent thread. The new thread inherits all of the definitions from the parent thread. If no parent thread is specified, the default thread is used. The default thread is where all of the "built-in" operators, functions, patterns, syntaxes, etc... are defined.
Releasing a thread
You can release a thread using ucReleaseItem with the handle of the thread. This will de-allocate all definitions associated with the thread. This can be compared to version 2.0's ucReset routine, except only the given thread is released. When you release a thread, all of its sub-threads are also released. Generally, it is best to avoid releasing the default thread, where the default functions and operators are defined.
Example:
' Default (parent) thread definition
ucDefine "Const: Pi = Atan(1)*4"
' Thread A definitions
tHandle_A = ucNewThread()
ucDefineFunction "COSINE(x) = COS(x)", 0, tHandle_A
ucDefineVariable "IndyVar = 20", 0,
tHandle_A
' Thread B definitions
tHandle_B = ucNewThread()
ucDefineFunction "COSINE(x) = COS(x * Pi /
180)", 0, tHandle_B
ucDefineVariable "IndyVar = 50", 0,
tHandle_B
' Printout
Print ucEval("Pi"), ucEval("Pi",
tHandle_A), ucEval("Pi", tHandle_B)
Print ucEval("IndyVar"),
ucEval("IndyVar", tHandle_A), ucEval("IndyVar", tHandle_B)
Print ucEval("COSINE(60)"),
ucEval("COSINE(60)", tHandle_A), ucEval("COSINE(60)",
tHandle_B)
If you run the above lines of code, you will get the
following results:
Expression Default thread
Thread A Thread B
Pi
3.14159...
3.14159... 3.14159...
IndyVar
Undefined 20 50
COSINE(60)
Undefined
-0.95241... 0.5