last day (17 days later) » 

9:50 AM
0
A: Call by value result or something else

Michael Aaron SafyanI think your professor may have used syntax that looks like Java for the sake of an example but, as others have noted, Java only supports pass by value. If, however, we are talking about a hypothetical language that simply has eerily similar syntax, then pass-by-value-result describes the desired...

 
why call by name or call by result are not preferred? would you please add a bit more detail /
you means because the computethread change value of x, and other threads need the initial value of x, we should choose pass by value result for computethread?
 
Pass-by-result is just shorthand for saying by-value-result, AFAIK. I've updated the answer to explain this more clearly. Pass-by-name is even more exotic and is more like how macros in C and C++ work. The main thing with pass-by-name is that the text of the parameter (including entire expressions) are substituted and lazily evaluated. It's better explained here: cs.sfu.ca/~cameron/Teaching/383/PassByName.html
@Dr.Jackson yes, that's correct. Since these need the initial value, using pass-by-value-result will make them see the original value, while still allowing the parameter to be updated by computethread.
 
and my last questions is 1) why for others use call by ref? 2)in pass by value result value of x changed after the thread is completed? thanks from your time. thanks. thanks again.
 
In this question, you wouldn't use pass-by-reference. You'd only use pass-by-value-result. You use pass-by-reference in other scenarios where you want to commit the modification immediately. Note that, in multithreading scenarios, using pass-by-reference also requires that reads/writes of the reference are synchronized (or that the item being written is volatile and written atomically) to ensure that reads do not see the value in an invalid, partially written state.
 
Dear @Michael, would you please describe: "multithreading scenarios, using pass-by-reference also requires that reads/writes of the reference are synchronized (or that the item being written is volatile and written atomically) to ensure that reads do not see the value in an invalid, partially written state"
 
9:53 AM
Your example involved multiple threads. If multiple threads access the same variable (i.e. they all share a reference, rather than a copy, to the same variable) and at least one of the threads writes to the variable, then it is necessary to protect reads/writes with a lock. To see why this is necessary, consider a 64-bit integer... on some platforms, writing to this integer will actually be done in two separate operations that transfer 32 bits at a time...
... in such a case, failure to use a "mutex" to synchronize the reading/writing would allow for the possibility of a thread seeing the value in a partially-written state (e.g. the most significant 32-bits could be from the old value, while the least significant 32-bits could be from the new value). Even for values that are written in a single operation, however, it's important to synchronize the read/write...
... due to caching behavior. For example, different threads may be associated with different CPU cores that maintain a local cache of these variables. Using thread synchronization primitives can force the cache to be flushed so that only the most recently written value is seen as opposed to a stale value.
 
 
2 hours later…
12:24 PM
are u here?
if our thread not edit the value, the call by ref is prefer to call by value? why?
 

  last day (17 days later) »