Mar 30, 2016; 20:54
Ke Carlton
Re: Dynamic thread names
I think it's important to highlight that while there's some similarities
between globals and thread objects that they are actually very different
beasts.
You will need to adjust your approach to fit within how thread objects
work.
It's a very bad idea to copy all the values from a thread object to local
variables; as when you do so it locks the thread while the data is copied
(it's always copied, never referenced).
It's far better to retrieve snippets of data as you need them as this
blocks the thread for less time. Obviously this varies from situation to
situation and the amount of data involved.
If you're using them to load settings for various sites, consider making a
parent type that will load the settings for that thread and have all your
threads inherit from that. For example:
define somethread => thread {
parent settingsloader
public oncreate => {
.load('somekey')
}
}
define anotherthread => thread {
parent settingsloader
public oncreate => {
.load('anotherkey')
}
}
In terms of addressing the threads dynamically you can do so with the
standard backslash invoke syntax:
local(
threadname = 'anotherthread',
thread = (\#threadname)()
)
You can also create helpers for this:
define get_thread(thread::tag) => (\#thread)()
define get_thread(thread::string) => (\#thread)()
Finally you could always dynamically create the threads; however I would
not recommend this approach as if you're starting to consider such routes
you're probably doing some important things fundamentally wrong:
define create_thread(thread::tag) => {
! #thread->istype
? sourcefile(`
define ` + #thread->asstring + ` => thread {
parent map
public oncreate => ..oncreate
}
`, #thread->asstring, false, false)()
return (\#thread)()
}
local(thread) = create_thread(::mythread) // returns a newly created or
existing mythread map thread
To summarise some of the major differences between globals and threads:
- Threads lock during both reads and writes (keep processing time low)
- Threads always copy data when returning data
- Threads are a defined type, whereas globals are an assigned value
- Once defined threads will always exist
Ke
On Thu, Mar 31, 2016 at 5:02 AM Da'oud Rashid <dzr@mac.com> wrote:
> Hi all,
>
> I’m in the process of upgrading from Lasso 8.5 to 9.x.
>
> I’m looking to replace my global vars with define threadName => somevalue .
>
> However, there are a number of instances in my old code where the global
> variable names are dynamic:
>
> Global($aVariableName + ’Something’ = $aVariable)
>
> Is it possible to do something similar for thread names? For example,
> something like the following (which doesn’t work):
>
> define ($myThreadName) => $aVariable->ascopydeep
>
> and
>
> $aVariable = thread($myThreadName)->ascopydeep
>
> I can do the following:
>
> define myMap => thread{
> parent map
>
> public oncreate => ..oncreate
>
> public load(map::map) => {
> #map->foreachpair => {
> self->insert(#1->first->ascopydeep > #1->second->ascopydeep)
> }
> }
> }
>
> myMaps->load($mapOfVars)
>
> However, I’d prefer to create separate threads….mainly so I don’t run into
> issues with a heavy volume of concurrent access to the threads. Also,
> iterating through each item in the map doesn’t seem like a very efficient
> way to decouple the thread from the thread variable.
>
> As an aside, I’m curious to know why ascopydeep doesn’t seem to work for
> threads (and doesn’t throw an error):
>
> local(
> item1Ref = 'item1',
> foo = 'unchanged'
> )
>
> var(
> myMap = map(#item1Ref = #foo),
> mySecondMap = $myMap->ascopydeep
> )
>
> define myThread => $myMap->ascopydeep
>
> $myMap->find(#item1Ref) = ‘changed'
>
> $mySecondMap->find(#item1Ref)
> // result: unchanged (as expected)
>
> myThread->Find(#item1Ref)
> // result: changed (unexpected)
>
> —— AND — —
>
> define myMap => thread{
> parent map
>
> public oncreate => ..oncreate
> }
>
> myMap->Insert(‘item1’ = ‘unchanged’)
>
> var(mySecondMap = myThread->ascopydeep)
>
> $mySecondMap->Find(’item1’) = ‘changed’
>
> $mySecondMap->Find(‘item1’)
> // result: changed (expected)
>
> myThread->Find(‘item1’)
> // result: changed (unexpected)
>
> Unfortunately, myMap->foreachpair => {…} doesn’t seem to work (it throws
> an error), so I can’t use that to copy values into a thread variable.
>
> Thank you,
> Da’oud
>
> #############################################################
>
> This message is sent to you because you are subscribed to
> the mailing list Lasso Lasso@lists.lassosoft.com
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
> Send administrative queries to <Lasso-request@lists.lassosoft.com>
#############################################################
This message is sent to you because you are subscribed to
the mailing list Lasso Lasso@lists.lassosoft.com
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
Send administrative queries to <Lasso-request@lists.lassosoft.com>