Changeset 62b5940
- Timestamp:
- Mar 20, 2025, 1:06:08 AM (36 hours ago)
- Branches:
- master
- Children:
- f5bf3c2
- Parents:
- 5329cba
- Location:
- doc/theses/mike_brooks_MMath
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/mike_brooks_MMath/programs/sharectx.run.cfa ¶
r5329cba r62b5940 1 1 #include <string.hfa> 2 2 #include <string_sharectx.hfa> 3 4 void a() {}5 void b() {}6 void c() {}7 void d() {}8 void e() {}9 3 10 4 … … 14 8 15 9 16 17 18 19 20 void helper2() { 21 c(); 22 string_sharectx c = {NEW_SHARING}; 23 d(); 10 void a( string & sm ) { 11 string sm_a = sm; // share 24 12 } 25 void helper1() { 26 a(); 27 string_sharectx c = {NO_SHARING}; 28 b(); 29 helper2(); 30 e(); 13 void b( string & sm ) { 14 string sm_b = sm; // share 15 } 16 void c( string & sm, string & sh1 ) { 17 string sm_c = sm; // copy 18 string sh1_c = sh1; // copy 19 } 20 void d( string & sm, string & sh1 ) { 21 string sm_d = sm; // copy 22 string sh1_d = sh1; // copy 23 } 24 void e( string & sm, string & sh1, string & sh2 ) { 25 string sm_e = sm; // copy 26 string sh1_e = sh1; // copy 27 string sh2_e = sh2; // share 28 } 29 void f( string & sm, string & sh1 ) { 30 string sm_f = sm; // copy 31 string sh1_f = sh1; // copy 32 } 33 void g( string & sm ) { 34 string sm_g = sm; // share 35 } 36 void helper2( string & sm, string & sh1 ) { 37 d( sm, sh1 ); 38 string_sharectx ctx_on = {NEW_SHARING}; 39 string sh2 = "sh2"; 40 e( sm, sh1, sh2 ); 41 } 42 void helper1( string & sm ) { 43 b( sm ); 44 string_sharectx ctx_off = {NO_SHARING}; 45 string sh1 = "sh1"; 46 c( sm, sh1 ); 47 helper2( sm, sh1 ); 48 f( sm, sh1 ); 31 49 } 32 50 int main() { 33 helper1(); 51 string sm = "sm"; 52 a( sm ); 53 helper1( sm ); 54 g( sm ); 34 55 } 35 -
TabularUnified doc/theses/mike_brooks_MMath/string.tex ¶
r5329cba r62b5940 470 470 Hence, concurrent users of string objects must still bring their own mutual exclusion, but the string library does not add any cross thread uses that are not apparent in a user's code. 471 471 472 \PAB{I could not get this to do anything.} 473 The \CFA string library does provides a @string_sharectx@ type to control an ambient sharing context for the current thread. 472 The \CFA string library provides the type @string_sharectx@to control an ambient sharing context for the current thread. 474 473 It allows two adjustments: to opt out of sharing entirely or to begin sharing within a private context. 475 474 Either way, the chosen mode applies only to the current thread, for the duration of the lifetime of the created @string_sharectx@ object, up to being suspended by child lifetimes of different contexts. 475 \VRef[Figure]{fig:string-sharectx} illustrates its behaviour. 476 Executing the example does not produce an interesting outcome. 477 But the comments indicate when the logical copy operation runs with 478 \begin{description} 479 \item[share:] the copy being deferred, as described through the rest of this section (fast), or 480 \item[copy:] the copy performed eagerly (slow). 481 \end{description} 482 Only eager copies can cross @string_sharectx@ boundaries. 476 483 The intended use is with stack-managed lifetimes, in which the established context lasts until the current function returns, and affects all functions called that do not create their own contexts. 477 \lstinputlisting[language=CFA, firstline=20, lastline=34]{sharectx.run.cfa}478 484 In this example, the single-letter functions are called in alphabetic order. 479 The functions @a@ and @d@ share string character ranges within themselves, but not with each other. 480 The functions @b@, @c@ and @e@ never share anything. 485 The functions @a@, @b@ and @g@ share string character ranges with each other, because they occupy a common sharing-enabled context. 486 The function @e@ shares within itself (because its is in a sharing-enabled context), but not with the rest of the program (because its context is not occupied by any of the rest of the program). 487 The functions @c@, @d@ and @f@ never share anything, because they are in a sharing-disabled context. 488 489 490 \begin{figure} 491 \begin{tabular}{ll} 492 \lstinputlisting[language=CFA, firstline=10, lastline=55]{sharectx.run.cfa} 493 & 494 \includegraphics{string-sharectx.pdf} 495 \end{tabular} 496 \caption{Controlling copying vs sharing of strings using \lstinline{string_sharectx}.} 497 \label{fig:string-sharectx} 498 \end{figure} 499 481 500 482 501 [ TODO: true up with ``is thread local'' (implement that and expand this discussion to give a concurrent example, or adjust this wording) ]
Note: See TracChangeset
for help on using the changeset viewer.