Execution model: Difference between revisions

Content deleted Content added
Kshalle (talk | contribs)
Kshalle (talk | contribs)
Line 66:
 
== Parallel Execution Models ==
In the modern age, parallel programming is an increasingly important topic. Parallel execution models tend to be complex because they involve multiple timelines. are simulated,Parallel andexecution themodels pointsnecessarily ininclude timethe atbehavior whichof activitiessynchronization inconstructs. one timelineA aresynchronization orderedconstruct relativehas tothe activitieseffect inof anotherestablishing timeline.an Forordering example,between theactivities [[C++11]]in standardone includestimeline sizeablerelative wordingto thatactivities dealsin withanother thetimeline. parallel execution model of the language.
 
For example, a common synchronization construct is the lock. Consider one timeline. The timeline has a point at which it executes the "gain ownership of the lock" synchronization construct. In Posix threads this would be pthread_mutex_lock(&myMutex). In Java this would be lock.lock(). In both cases, the timeline is called a thread. The C and Java execution models are sequential, and they state that the timeline has activities that come before the call to "gain ownership of the lock", and activities that come after the call. Likewise there is a "give up ownership of the lock" operation. In C this would be pthread_mutex_unlock(&myMutex). In Java this would be lock.unlock(). Again, the execution models C and Java define that are statements executed before ownership of the lock is given up, and statements executed after ownership of the lock is given up.
 
Now, consider the case of two timelines (two threads). One thread, call it thread A, executes some statements, call them A-pre-gain-lock statements. Then thread A executes "gain ownership of the lock", then thread A executes A-post-gain-lock statements, which come after A gains ownership of the lock. Finally, thread A performs "give up ownership of the lock". Then thread A performs A-post-giveup-lock statements.
 
A second thread, call it thread b, executes some statements, call them B-pre-lock statements. Then thread B executes "gain ownership of the lock", then thread B executes B-post-lock statements, which come after B gains ownership of the lock.
 
Finally, the parallel execution model of the "gain ownership of lock" and "give up ownership of lock" synchronization construct is this:
 
"In the case that ownership of the lock goes from thread A to thread B, A-post-gain-lock statements come before B-post-gain-lock statements."
 
And that's it.
 
Seems simple, right? The complication comes from the fact that the execution model does not have any means for the execution of "give up ownership of the lock" to have any influence over which execution of "gain ownership of the lock" in some other timeline (thread) follows. Very often, only certain handoffs give valid results. Thus, the programmer must think of all possible combinations of one thread giving up a lock and another thread getting it next, and make sure their code only allows valid combinations.
 
== See also ==