C++ Library Reference

Obtaining Characters Extracted by Last Unformatted Input Operation

To be MT-safe, the gcount function must be called within a thread that has exclusive use of the istream object for the period that includes the execution of the last input operation and gcount call. The following example shows a call to gcount:


Example 4-2 Calling gcount

#include <iostream.h>
#include <rlocks.h>
void fetch_line(istream& istr, char* line, int& linecount)
{
	stream_locker sl(istr, stream_locker::lock_defer);

	sl.lock(); // lock the stream istr
	istr >> line;
	linecount = istr.gcount();
	sl.unlock(); // unlock istr
	...
}

In this example, the lock and unlock member functions of class stream_locker define a mutual exclusion region in the program.