There are several advanced services available for any
NetStream
object. These services are specially important
for solving synchronization tasks, namely establishing
synchonization points (called barriers), broadcasting one
message to the rest of processes, and checking whether there is a
pending message in the regular" or \verb"packed
stream. The
corresponding methods in the NetStream
class are
(respectively) _barrier()", \verb"_broadcast()
, and
_probe()
. As before, there exist methods with the same name
and behavior that can be used as manipulators with the <<
and >>
operators. See the following code to learn
the syntax of the NetStream
methods:
class NetStream { public: ... // BASIC SERVICES already described NetStream& _pack_begin(void); // Marks the beginning of a packed information NetStream& _pack_end(void); // Marks the end of a packed and flush it to the net NetStream& _probe(const int stream_type, int& pending); // Check whether there are awaiting data NetStream& _broadcast(void); // Broadcast a message to all the processes NetStream& _barrier(void); // Sit and wait until all processes are in barrier ... };
When programming for a LAN environment, passing basic C++
types such as int" or \verb"double
is OK with modern
technologies, since the latency is low. However, for a WAN
environment sending many continuous messages with such basic types
could provoke an unnecessary delay in communications. Network
resources can be better exploited if the user define data
packets.
Defining a data packet is very easy because only the manipulators
pack_beginand
pack_endmust be used. All the output operations in between these two reserve words are put inside the same physical packet, with the ensuing savings in time. The contents of the packet are not forced to share the same base object class or type, thus improving the flexibility of this construction.
if(mypid==0) // The sending process { ... strcpy(str,"this is sent inside a heterogeneous packet"); netstream << pack_begin << str << 9.9 << 'z' << pack_end; ... } else // The receiving process { ... netstream << wait(packed); // Wait for a packed message netstream << pack_begin // Reads the packed message >> str >> d >> c << pack_end; ... }