ThreadLocal-like in Pharo Smalltalk

Is there a Pharo equivalent for Java ThreadLocals or a way to achieve this behavior? For example, in Hibernate ThreadLocals are used to provide a thread (current request / context) of "limited" unity of a working instance - called Session on Hibernate - through one call to the getCurrentSession method. The developer does not need to worry and just assume that the method will return the correct unit of work. Is this possible on Pharo?

I looked at this on Pharo Books (Pharo on the example of Pharo enterprise and Deep Pharo) and on this page , but could not find useful information.

+4
source share
1 answer

in Pharo, you are using a subclass ProcessLocalVariable. For instance:

"Create a class to store you variable"
ProcessLocalVariable subclass: #MyVariable.

"Use it like this"
MyVariable value: myValue.

"Inside your code, access to current value like this"
MyVariable value.  

Note that even more powerful than local thread variables, you have “dynamic variables” that refer to the execution stack (more precisely than threads) you use it like this:

"Create a class to store you variable"
DynamicVariable subclass: #MyVariable.

"Use it like this"
MyVariable 
    value: myValue 
    during: [ 
        "... execute your code here... usually a message send"
        self doMyCode ].

"Inside your code, access to current value like this"
MyVariable value.  

Such variables offer the same functionality (they are even more powerful) and are usually best replaced.

+7
source

Source: https://habr.com/ru/post/1670947/


All Articles