- Timestamp:
- Oct 10, 2021, 5:11:28 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- a73c16e
- Parents:
- 321a1b15
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r321a1b15 rd4d4ac8 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon May 31 09:03:34202114 %% Update Count : 50 7113 %% Last Modified On : Sun Oct 10 12:45:00 2021 14 %% Update Count : 5095 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 4444 4444 \CFA provides a fine-grained solution where a \Index{recursive lock} is acquired and released indirectly via a manipulator ©acquire© or instantiating an \Index{RAII} type specific for the kind of stream: ©osacquire©\index{ostream@©ostream©!osacquire@©osacquire©} for output streams and ©isacquire©\index{isacquire@©isacquire©}\index{istream@©istream©!isacquire@©isacquire©} for input streams. 4445 4445 4446 The common usage is manipulator ©acquire©\index{ostream@©ostream©!acquire@©acquire©} to lock a stream during a single cascaded I/O expression, with the manipulator appearing as the first item in a cascade list, \eg:4447 \begin{cfa} 4448 $\emph{thread\(_1\)}$ : sout | ®acquire® | "abc " | "def "; // manipulator4449 $\emph{thread\(_2\)}$ : sout | ®acquire®| "uvw " | "xyz ";4446 The common usage is the short form of the mutex statement\index{ostream@©ostream©!mutex@©mutex©} to lock a stream during a single cascaded I/O expression, \eg: 4447 \begin{cfa} 4448 $\emph{thread\(_1\)}$ : ®mutex()® sout | "abc " | "def "; 4449 $\emph{thread\(_2\)}$ : ®mutex()® sout | "uvw " | "xyz "; 4450 4450 \end{cfa} 4451 4451 Now, the order of the thread execution is still non-deterministic, but the output is constrained to two possible lines in either order. … … 4466 4466 In summary, the stream lock is acquired by the ©acquire© manipulator and implicitly released at the end of the cascaded I/O expression ensuring all operations in the expression occur atomically. 4467 4467 4468 To lock a stream across multiple I/O operations, an object of type ©osacquire© or ©isacquire© is declared to implicitly acquire/release the stream lock providing mutual exclusion for the object's duration, \eg: 4469 \begin{cfa} 4470 { // acquire sout for block duration 4471 ®osacquire® acq = { sout }; $\C{// named stream locker}$ 4468 To lock a stream across multiple I/O operations, he long form of the mutex statement is used, \eg: 4469 \begin{cfa} 4470 ®mutex( sout )® { 4472 4471 sout | 1; 4473 sout | ®acquire® | 2 | 3; $\C{// unnecessary, but ok to acquire and release again}$4472 ®mutex() sout® | 2 | 3; $\C{// unnecessary, but ok because of recursive lock}$ 4474 4473 sout | 4; 4475 } // implicitly release the lock when "acq" is deallocated4476 \end{cfa} 4477 Note, the unnecessary © acquire© manipulatorworks because the recursive stream-lock can be acquired/released multiple times by the owner thread.4474 } // implicitly release sout lock 4475 \end{cfa} 4476 Note, the unnecessary ©mutex© in the middle of the mutex statement, works because the recursive stream-lock can be acquired/released multiple times by the owner thread. 4478 4477 Hence, calls to functions that also acquire a stream lock for their output do not result in \Index{deadlock}. 4479 4478 4480 4479 The previous values written by threads 1 and 2 can be read in concurrently: 4481 4480 \begin{cfa} 4482 { // acquire sin lock for block duration 4483 ®isacquire acq = { sin };® $\C{// named stream locker}$ 4481 ®mutex( sin )® { 4484 4482 int x, y, z, w; 4485 4483 sin | x; 4486 sin | ®acquire® | y | z; $\C{// unnecessary, but ok to acquire and release again}$4484 ®mutex() sin® | y | z; $\C{// unnecessary, but ok because of recursive lock}$ 4487 4485 sin | w; 4488 } // implicitly release the lock when "acq" is deallocated4486 } // implicitly release sin lock 4489 4487 \end{cfa} 4490 4488 Again, the order of the reading threads is non-deterministic. … … 4493 4491 \Textbf{WARNING:} The general problem of \Index{nested locking} can occur if routines are called in an I/O sequence that block, \eg: 4494 4492 \begin{cfa} 4495 sout | ®acquire® | "data:" | rtn( mon ); $\C{// mutex call on monitor}$4493 ®mutex() sout® | "data:" | rtn( mon ); $\C{// mutex call on monitor}$ 4496 4494 \end{cfa} 4497 4495 If the thread executing the I/O expression blocks in the monitor with the ©sout© lock, other threads writing to ©sout© also block until the thread holding the lock is unblocked and releases it. … … 4500 4498 \begin{cfa} 4501 4499 int ®data® = rtn( mon ); 4502 sout | acquire| "data:" | ®data®;4500 mutex() sout | "data:" | ®data®; 4503 4501 \end{cfa} 4504 4502 … … 4506 4504 \section{String Stream} 4507 4505 4508 All the stream formatting capabilities are available to format text to/from a C string rather than to a stream file. 4509 \VRef[Figure]{f:StringStreamProcessing} shows writing (output) and reading (input) from a C string. 4506 The stream types ©ostrstream© and ©istrstream© provide all the stream formatting capabilities to/from a C string rather than a stream file. 4507 \VRef[Figure]{f:StringStreamProcessing} shows writing (output) to and reading (input) from a C string. 4508 The only string stream operations different from a file stream are: 4509 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt] 4510 \item 4511 constructors to create a stream that writes to a write buffer (©ostrstream©) of ©size©, or reads from a read buffer (©istrstream©) containing a C string terminated with ©'\0'©. 4512 \begin{cfa} 4513 void ?{}( ostrstream &, char buf[], size_t size ); 4514 void ?{}( istrstream & is, char buf[] ); 4515 \end{cfa} 4516 \item 4517 \Indexc{write} (©ostrstream© only) writes all the buffered characters to the specified stream (©stdout© default). 4518 \begin{cfa} 4519 ostrstream & write( ostrstream & os, FILE * stream = stdout ); 4520 \end{cfa} 4521 There is no ©read© for ©istrstream©. 4522 \end{itemize} 4523 4510 4524 \begin{figure} 4511 4525 \begin{cfa} … … 4520 4534 double x = 12345678.9, y = 98765.4321e-11; 4521 4535 4522 osstr | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) ; $\C{// same lines of output}$4523 write( osstr ); 4524 printf( "%s", buf ); 4525 sout | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) ;4526 4527 char buf2[] = "12 14 15 3.5 7e4 "; $\C{// input buffer}$4536 osstr | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc"; 4537 write( osstr ); $\C{// write string to stdout}$ 4538 printf( "%s", buf ); $\C{// same lines of output}$ 4539 sout | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc"; 4540 4541 char buf2[] = "12 14 15 3.5 7e4 abc"; $\C{// input buffer}$ 4528 4542 ®istrstream isstr = { buf2 };® 4529 isstr | i | j | k | x | y; 4530 sout | i | j | k | x | y; 4531 } 4543 char s[10]; 4544 isstr | i | j | k | x | y | s; 4545 sout | i | j | k | x | y | s; 4546 } 4547 4548 3 0x5 7 1.234568e+07 987.654n abc 4549 3 0x5 7 1.234568e+07 987.654n abc 4550 3 0x5 7 1.234568e+07 987.654n abc 4551 12 14 15 3.5 70000. abc 4532 4552 \end{cfa} 4533 4553 \caption{String Stream Processing} 4534 4554 \label{f:StringStreamProcessing} 4535 4555 \end{figure} 4536 4537 \VRef[Figure]{f:StringStreamFunctions} shows the string stream operations.4538 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]4539 \item4540 \Indexc{write} (©ostrstream© only) writes all the buffered characters to the specified stream (©stdout© default).4541 \end{itemize}4542 The constructor functions:4543 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]4544 \item4545 create a bound stream to a write buffer (©ostrstream©) of ©size© or a read buffer (©istrstream©) containing a C string terminated with ©'\0'©.4546 \end{itemize}4547 4548 \begin{figure}4549 \begin{cfa}4550 // *********************************** ostrstream ***********************************4551 4552 ostrstream & write( ostrstream & os, FILE * stream = stdout );4553 4554 void ?{}( ostrstream &, char buf[], size_t size );4555 4556 // *********************************** istrstream ***********************************4557 4558 void ?{}( istrstream & is, char buf[] );4559 \end{cfa}4560 \caption{String Stream Functions}4561 \label{f:StringStreamFunctions}4562 \end{figure}4563 4564 4556 4565 4557 \begin{comment}
Note: See TracChangeset
for help on using the changeset viewer.