C++ Library Reference

Checking Error State

To be MT-safe, error checking must occur in a critical region with the I/O operation that causes the error. The following example illustrates how to check for errors:


Example 4-1 Checking Error State

#include <iostream.h>
enumm iostate { IOok, IOeof, IOfail };

iostate read_number(istream& istr, int& num)
{
	stream_locker sl(istr, stream_locker::lock_now);

	istr >> num;

	if (istr.eof()) return IOeof;
	if (istr.fail()) return IOfail;
	return IOok;
}

In this example, the constructor of the stream_locker object sl locks the istream object istr. The destructor of sl, called at the termination of read_number, unlocks istr.