flash - In Actionscript 3, what happens if different events change the value of a variable at the same time? -
what happens if 2 events occur @ same time change value of variable?
for instance, have 2 objects: a
, b
, loaded stage
, stage variable named varx
.
the following code embedded in a
's fifth frame:
stage.varx = 9
in other hand, have, in b
's fifth frame, following code:
stage.varx = 2
so, what'll varx
's value, when both movieclips reach fifth frame?
tl;dr
a
, b
won't enter fifth frame @ same exact time, unless you're doing unusual. either a
or b
first, , whichever come last overwrite first one's value. make sure don't depend
on them firing in order. you're doing unusual, it's still non-issue.
this answered assuming you're referring single program multiple objects in stage. if you're talking multiple programs trying share sort of global stage
state instead (i'm not sure whether share same stage
object or not), can't sure, work same way.
flash generally not use multi-threading. in fact, as3 did not suport @ until maybe 2 years ago, give or take. 99% of time, swfs not use it.
in case, a
, b
not enter fifth frames @ same time. it's impossible. 1 has enter fifth frame first, other. requires multithreading change fact within single program.
so let's have event handlers that'll fire when each enters fifth frame. happen:
- the first 1 enter frame so.
- then raise event.
- then handler object catch event.
- then handler set
stage
variable whatever value gave particular handler. - then, once that's done, other object enter fifth frame.
one thing events in as3 they're not closely tied central core of language might expect when first start out. they're class in "usual api" have had part of api built around them. when handle event, happens code decides wants dispatch event. creates instance of class event
, calls 1 of functions called dispatchevent
, passing in event
argument. dispatchevent
looks through list of callback functions , starts calling them 1 after another, passing event
argument each of them. 1 of callbacks event handler. 1 callback called and returns, next, next. whereas event-handling asynchronous whole, dispatching , handling of events carried out in synchronous manner.
the exception one-thing-at-a-time rule when use worker threads in more modern versions of flash. may little closer multiprocessing multithreading (arguably blend of two), requires use of secondary, embedded swfs communicate through main swf through "workers". yes, perhaps run situation here both try mess same variable @ same time.
this common problem in computer science, short answer this: platforms program on top of, whether they're flash or .net or whatever, designed block being able write exact same variable and/or memory address @ same exact time 2 different sources. unless decide go lower-level, can assume platform have own locking mechanisms internally built in prevent simultaneous writes. applies here.
however there 2 caveats this:
this applies one specific variable / memory address. not apply to, let's say, internal state of entire string. textbook problem multithreading in language c++ if 2 threads try concatenate on same exact string variable @ same exact time. in c++, string quite literally array of
chars
(or instancestd::string
, wrapper around this) ends when gets element integer value0
. 2 threads trying add characters string might start @ different times, both starting before first finished, @ point second thread changing value of characterx
while first changing value of characterx + y
. in higher-level languages though, there safeguards built in case of strings, can still run when multiple variables changed within given object.race conditions. vesper touched on this. while
a
,b
may not changestage.x
@ same time, still don't quite know order they're going change in. , not built platform, either in flash or in of else. when have that's asynchronous, if it's on surface , in single-threaded environment, have place logic there make sure other stuff not dependent on being executed in particular order.
this holds computer science in general. may know of this, answer nothing unusual happen stage.x
; it's can't tell value come first or last.
Comments
Post a Comment