>Alberto Moreira <email@example.com> writes: > >> For example, >> something as simple as a function to add two numbers would be f(a,b) >> in mathematics. But inside a program, it means "load a, load b, add >> them, put them in a register, and return that register to the >> caller: > >I thought you were saying that modern programming is all object >oriented. You seem to have forgotten one of the keywords: >encapsulation. > >The program does not say "load a...". Even most of the non-object >oriented languages do not say this. This level of detail is in the >compiler (or interpreter), and quite possibly hardware-dependent.
Kevin, it's a COMPUTER, not an idealization inside a brainwave. Values a and b, be them objects or whatever, reside in a MEMORY. If you use an object, you have to READ it before you use it. If you update an object, you must READ it, UPDATE it, then WRITE it. This is not about hardware dependency, this is about the fundamental nature of computer data: data just doesn't exist as abstractions, data inside computers is concrete, it has LOCATION, LIFETIME, and SCOPE. In math, 3 is 3 no matter what - inside a computer, 3 here and 3 there are NOT the same 3.
Say I have a function f to add two numbers:
f(x,y) = x+y;
Say I have two threads running concurrently, both use f( ) to bump the value of a. One says
a = f(a,3)
The other says
a = f(a,1)
I'm already abusing of mathematical notation and writing a=a+b for the statement that a *becomes* a+b, but let it be, there's more fundamental stuff under this hood.
In mathematics, the result of those two operations, carried out in either sequence, is a+4. Inside a computer, the result can be a+1, a+3 or a+4, not because of hardware or compilers, but because of the intrinsic nature of stored data. If we blow the function out into its atomic components, it really looks like this:
r1=a; r1=r1+3; a = r1; r2=a; r2=r2+1; a =r2;
Where the ri are machine registers, and each operation that ends with a semicolon is indeed atomic. The interweaving of atomic operations takes care of the nondeterminism:
r1=a; r2=a; r1=r1+3; r2=r2+1; a=r2; a=r1;
and you get a+3.
r1=a; r2=a; r1=r1+3; r2=r2+1; a=r1; r=r2;
and you get a+1. So, it isn't enough to reduce things to mathematics, we must go one level down.
So, Kevin, ANYTIME WE HAVE SHARED WRITABLE DATA WE HAVE SYNCHRONIZATION PROBLEMS - that's the beginning of Operating Systems 101. That's why in computer science we have mutexes, semaphores, message passing, channels, critical sections, events, monitors, and all that other jazz. True, you can throw math at it, but by the time you get to real results, the math has gotten pretty hairy and its usability is limited to the most trivial cases because of the combinatorial explosion of the model's complexity.