Threading of Function Parameters

Post » Fri Mar 14, 2014 12:04 am

When a script calls an external function and passes one of its variables to that function, does this result in the function parameter being immediately passed or put onto the stack along with the function call, or does the passing of the variable have to wait for the queue to "accept" it?

An example will probably make my question clearer:

Weapon weapEvent OnObjectEquipped(Form baseObj, ObjectReference ref)    weap = baseObj as Weapon    externalWeaponFunction(weap)EndEvent

In the example above, will the current "weap" always be passed to externalWeaponFunction in a threadsafe manner? Or will this thread have to wait before passing the parameter, in which case it would seem "weap" could be changed by a subsequent OnObjectEquipped event, before it actually gets passed to externalWeaponFunction.

I would imagine it's possible for the whole function call including its parameter to be put on the stack to wait in the thread queue, but I'm not sure if that's how it works or not.

User avatar
Aaron Clark
 
Posts: 3439
Joined: Fri Oct 26, 2007 2:23 pm

Post » Fri Mar 14, 2014 5:15 am

A function doesn't ask for the value for its parameters until it successfully starts running. So in your above example, it wouldn't get the value of "weap" until "exteranlWeaponFunction" started.

It's not entirely cut and dry due to the interaction of save games, whether the target object forces the call to wait or not, and whether the compiler substituted temporary variables that you're not seeing in your code (though that usually only happens when the compiler has to put in a cast or you've put an expression in the parameter list).

If you're honestly worried about a case like this the best solution would be to make sure that you're only passing stack variables to the function, since stack variables are specific to the thread and cannot be modified by other threads. (Caveat: Object variables or Array variables won't change what they point at, but because they're held onto by reference, someone can modify the object or array itself - but that's not specific to function calling)
User avatar
Robert Jr
 
Posts: 3447
Joined: Fri Nov 23, 2007 7:49 pm

Post » Thu Mar 13, 2014 9:52 pm

Thanks SmkViper for the clarification.

Not so much worried as I am just curious about how it works :turned:

User avatar
Kelly Osbourne Kelly
 
Posts: 3426
Joined: Sun Nov 05, 2006 6:56 pm


Return to V - Skyrim