#include <mcs.hh>
This class can be used to perform communication between threads using system pipes. There must always be two threads: a "producer" (write) and a "consumer" (read) and the same Pipe object must be accessible from both threads.
The communication is initiated by one of the threads through the create() or createNamed() methods, while the other is waiting until the isReady() method returns true (and maybe using sleep_ms()). When the create() method is used an unnamed pipe will be created and both threads will read/write using file descriptors. If createNamed() method is used then a FIFO special file will be created and (only) one thread will access the pipe as if it was a normal file (that is using open, or fopen, etc...). The filename for the FIFO is generated automatically and can be retrieved wih the filename() method.
The consumer thread obtain the file descriptor to read using the openRead() method, the producer thread obtain the file descriptor to write using the openWrite() method. To each openRead() (or openWrite()) call must correspond one closeRead() (or closeWrite()) call.
If the producer thread is using the file descriptor obtained through openWrite() then it can check whether the other side of the pipe (namely the consumer side) is still open by calling the consumerHasGone() method.
Definition at line 7718 of file mcs.hh.
Public Member Functions | |
void | closeRead () |
Close the file descriptor associated with the consumer side of the pipe. | |
void | closeWrite () |
Close the file descriptor associated with the producer side of the pipe. | |
bool | consumerHasGone () |
Tell if the consumer thread is still reading. | |
void | create () |
Create an unnamed pipe, both threads will read/write using file descriptors. | |
string | createNamed () |
Create a named pipe, that is a FIFO special file and returns the filename. | |
string | filename () |
If the pipe has been set-up through the createNamed() method this returns the generated file name. | |
bool | isReady () |
Returns true if the pipe has been set-up (through the create() or createNamed() methods). | |
int | openRead () |
Return the file descriptor for the consumer side of the pipe. | |
int | openWrite () |
Return the file descriptor for the producer side of the pipe. | |
Pipe () | |
Constructor. | |
~Pipe () | |
Destructor. | |
Private Attributes | |
bool | flcreated |
Tell if a pipe has been created. | |
bool | named |
If it is a named pipe (a FIFO file) or not. | |
int | pipefd [2] |
Pipe file descriptors (0 read side, 1 write side). | |
string | pipefn |
Name of the FIFO file. | |
Static Private Attributes | |
static int | filecount = 0 |
Used to generate a unique filename. | |
static Synchro | synchro |
To protect the filecount variable. |
Pipe::Pipe | ( | ) |
Pipe::~Pipe | ( | ) |
void Pipe::closeRead | ( | ) |
Close the file descriptor associated with the consumer side of the pipe.
Definition at line 168 of file Readers.cc.
void Pipe::closeWrite | ( | ) |
Close the file descriptor associated with the producer side of the pipe.
Definition at line 184 of file Readers.cc.
bool Pipe::consumerHasGone | ( | ) |
Tell if the consumer thread is still reading.
This method can be used only if the producer thread is using the file descriptor obtained through openWrite(). In this case the producer should check that the consumer is still reading before each write attempt.
Definition at line 61 of file Readers.cc.
void Pipe::create | ( | ) |
Create an unnamed pipe, both threads will read/write using file descriptors.
Definition at line 71 of file Readers.cc.
string Pipe::createNamed | ( | ) |
Create a named pipe, that is a FIFO special file and returns the filename.
Definition at line 86 of file Readers.cc.
string Pipe::filename | ( | ) |
If the pipe has been set-up through the createNamed() method this returns the generated file name.
Definition at line 57 of file Readers.cc.
bool Pipe::isReady | ( | ) |
Returns true if the pipe has been set-up (through the create() or createNamed() methods).
Definition at line 54 of file Readers.cc.
int Pipe::openRead | ( | ) |
Return the file descriptor for the consumer side of the pipe.
Definition at line 128 of file Readers.cc.
int Pipe::openWrite | ( | ) |
Return the file descriptor for the producer side of the pipe.
Definition at line 148 of file Readers.cc.
int Pipe::filecount = 0 [static, private] |
bool mcs::Pipe::flcreated [private] |
Tell if a pipe has been created.
A pipe will be created when a create() or createNamed() method is invoked. Note that if a file is in the local filesystem the pipe won't be created.
bool mcs::Pipe::named [private] |
int mcs::Pipe::pipefd[2] [private] |
string mcs::Pipe::pipefn [private] |
Synchro Pipe::synchro [static, private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|