C++ Library Reference

User-Defined I/O Operations

To be MT-safe, I/O operations defined for a user-defined type that involve a specific ordering of separate operations must be locked to define a critical region. The following example shows a user-defined I/O operation:


Example 4-3 User-Defined I/O Operations

#include <rlocks.h>
#include <iostream.h>
class mystream: public istream {

	// other definitions...
	int getRecord(char* name, int& id, float& gpa);
};

int mystream::getRecord(char* name, int& id, float& gpa)
{
	stream_locker sl(this, stream_locker::lock_now);

	*this >> name;
	*this >> id;
	*this >> gpa;

	return this->fail() == 0;
}