stream_locker man page on SunOS

Man page or keyword search:  
man Server   20652 pages
apropos Keyword Search (all sections)
Output format
SunOS logo
[printable version]

stream_locker(3CC4)					   stream_locker(3CC4)

NAME
       stream_locker  -	 class	used for application level locking of iostream
       class objects.

SYNOPSIS
       #include <rlocks.h>

       class stream_locker {
       public:
	    enum lock_choice { lock_defer=0, lock_now=1	 };
	    stream_locker(stream_MT&, lock_choice lock_flag=lock_now);
	    stream_locker(stream_MT*, lock_choice lock_flag=lock_now);
	    stream_locker(stream_rmutex&, lock_choice lock_flag=lock_now);
	    stream_locker(stream_rmutex*, lock_choice lock_flag=lock_now);
	    ~stream_locker();
	    void lock();
	    void unlock();
       };

DESCRIPTION
       Instances of class  stream_locker  are  used  to	 implement  an	atomic
       sequence	  of   operations  on  a  iostream  class  object.  The	 class
       stream_locker is a new feature of the multi-thread (MT) safe  libC.  It
       can  be	used  to define a region of mutual exclusion in which only one
       thread can perform operations on an iostream object at any one time.

       A stream_locker object applies on a per thread basis. When  one	thread
       in  a  process locks an iostream object other threads are blocked while
       trying to use that iostream  object.   The  scope  of  a	 stream_locker
       object can be used to define the bounds of the mutual exclusion region.
       A stream_locker should only be created locally. It is  not  recommended
       to create a stream_locker with global or static scope.

       When a stream_locker object is created locally, its constructor defines
       the beginning of the mutual exclusion region and the destructor defines
       the  end	 of the region. Alternatively lock() and unlock() member func‐
       tions are provided to define explicitly the bounds of a	mutual	exclu‐
       sion region.

   Constructors
       stream_locker(stream_MT&, lock_choice lock_flag=lock_now);
       stream_locker(stream_MT*, lock_choice lock_flag=lock_now);

	      All  iostream classes in the MT safe version of libC are derived
	      from class stream_MT. The constructor does the locking  for  the
	      stream given as an argument.

	      The  lock-choice	flag indicates how the mutual exclusion region
	      is to be defined. A  value  of  lock_defer  indicates  that  the
	      region  is to be defined by the lock() and unlock() member func‐
	      tions. A value of lock_now indicates that the region is  defined
	      by  the constructor and destructor of stream_locker. The default
	      value of the lock_choice is lock_now.

       stream_locker(stream_rmutex&,	 lock_choice	  lock_flag=lock_now);
       stream_locker(stream_rmutex*, lock_choice lock_flag=lock_now);

	      Class  stream_rmutex  is	the  recursive	mutex used by iostream
	      classes. It is not available for use by applications.

   Member functions
       void lock();
       void unlock();
       Examples:

       In this first example a stream_locker object ensures that a seek	 to  a
       particular  offset  in  a  file	and a read from the file is atomic and
       there is no possibility of another  thread  changing  the  file	offset
       before the original thread reads the file.

       {    fstream fs;
	    stream_locker s_lock(fs); // lock the stream fs
	    . . . . .// open file
	    fs.seekg(offset, ios::beg);
	    fs.read(buf, len);
       }
       The  constructor	 of  object s_lock locks the fstream object fs and the
       destructor, executed at end of scope, unlocks fs.

       The second example illustrates the use of explicit  definition  of  the
       mutual exclusion region using lock() and unlock() member function calls
       of a stream_locker object.

       const int bufsize = 256;
       void print_tagged_lines(char *filename, int thread_tag) {
	    char ibuf[bufsize+1];
	    stream_locker  lockout(cout, stream_locker::lock_defer);
	    ifstream instr(filename);

	    while(1) {
		 // read a line at a time
		 instr.getline (ibuf, bufsize, ´\n´);
		 if (instr.eof())
		      break;
		 // lock cout stream so the i/o operation is atomic
		 lockout.lock();
		 // tag line and send to cout
		 cout << thread_tag << ibuf << "\n";
		 lockout.unlock();
	    }
       }

ERRORS
       Stream_locker class objects are bracketing so if the end	 of  a	mutual
       exclusion  region  is not defined implicitly or explicitly it is a user
       error and the result cannot be determined.

				1 February 1994		   stream_locker(3CC4)
[top]

List of man pages available for SunOS

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net