({{, (RefLink[CreateManagedLibraryExpression, paclet:ref/CreateManagedLibraryExpression])[mname,f] 
creates a managed library expression by applying f to a positive integer ID associated with a registered manager with name mname.}})

OpenersNotesSection(Spacer1)Details

The IDs generated for the manager with name mname start at 1 and follow sequentially until the manager is unregistered.

A manager is registered by using the LibraryLink callback function (NonWLLink[registerLibraryExpressionManager, paclet:LibraryLink/ref/callback/registerLibraryExpressionManager]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (1)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Load example (RefLink[LibraryFunction, paclet:ref/LibraryFunction]) objects that implement a simple linear congruential random generator:

setInstanceState = LibraryFunctionLoad["demo_managed","setInstanceState",{Integer, {Integer, 1}},"Void"];
generateFromInstance = LibraryFunctionLoad["demo_managed", "generateFromInstance",{Integer,{Integer, _}},{Real,_}];

Create an instance of a linear congruential generator:

g1=CreateManagedLibraryExpression["LCG",randomLCG]

Set its parameters using the unique ID assigned by (RefLink[CreateManagedLibraryExpression, paclet:ref/CreateManagedLibraryExpression]):

setInstanceState[ManagedLibraryExpressionID[g1],{1664525,1013904223,2^32,0}]

Generate two pseudorandom numbers from the instance:

generateFromInstance[ManagedLibraryExpressionID[g1],{2}]

Set up another instance and generate three numbers from it:

g2=CreateManagedLibraryExpression["LCG",randomLCG];
setInstanceState[ManagedLibraryExpressionID[g2],{1664525,1013904223,2^32,0}];
generateFromInstance[ManagedLibraryExpressionID[g2],{3}]

The first instance is available until the expression is no longer referenced:

generateFromInstance[ManagedLibraryExpressionID[g1],{4}]

The history expression (RefLink[Out, paclet:ref/Out]) will often keep references to an expression you may not be aware of. Extra references like this can be prevented by setting (RefLink[$HistoryLength, paclet:ref/$HistoryLength])=0 either in a session or inside of (RefLink[Block, paclet:ref/Block]):

Block[{$HistoryLength = 0}, g3 = CreateManagedLibraryExpression["LCG",randomLCG];];
g3str = ToString[g3]

The variable g3 is the only reference to the managed library instance, so when g3 is unset, the library instance is freed and expressions with this ID will no longer work:

{ManagedLibraryExpressionQ[ToExpression[g3str]],Unset[g3];ManagedLibraryExpressionQ[ToExpression[g3str]]}

Note that the same does not work for g1 because it is referenced by (RefLink[Out, paclet:ref/Out]):

g1str = ToString[g1];
{ManagedLibraryExpressionQ[ToExpression[g1str]],Unset[g1];ManagedLibraryExpressionQ[ToExpression[g1str]]}

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ManagedLibraryExpressionQ, paclet:ref/ManagedLibraryExpressionQ])  ▪  (RefLink[ManagedLibraryExpressionID, paclet:ref/ManagedLibraryExpressionID])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[LibraryLink User Guide, paclet:LibraryLink/tutorial/Overview])}, {▪(RefLinkPlain[Managed Library Expressions, paclet:LibraryLink/tutorial/InteractionWithWolframLanguage#353220453])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Wolfram LibraryLink, paclet:guide/LibraryLink])}})}})