MCS
0.3.3-alpha7
|
A high level class to use system pipes. More...
#include <mcs.hh>
Public Member Functions | |
void | closeRead () |
Close the file descriptor associated with the consumer side of the pipe. More... | |
void | closeWrite () |
Close the file descriptor associated with the producer side of the pipe. More... | |
bool | consumerHasGone () |
Tell if the consumer thread is still reading. More... | |
void | create () |
Create an unnamed pipe, both threads will read/write using file descriptors. More... | |
string | createNamed () |
Create a named pipe, that is a FIFO special file and returns the filename. More... | |
string | filename () |
If the pipe has been set-up through the createNamed() method this returns the generated file name. More... | |
bool | isReady () |
Returns true if the pipe has been set-up (through the create() or createNamed() methods). More... | |
int | openRead () |
Return the file descriptor for the consumer side of the pipe. More... | |
int | openWrite () |
Return the file descriptor for the producer side of the pipe. More... | |
Pipe () | |
Constructor. More... | |
~Pipe () | |
Destructor. More... | |
Private Attributes | |
bool | flcreated |
Tell if a pipe has been created. More... | |
bool | named |
If it is a named pipe (a FIFO file) or not. More... | |
int | pipefd [2] |
Pipe file descriptors (0 read side, 1 write side). More... | |
string | pipefn |
Name of the FIFO file. More... | |
Synchro | synchro |
To protect the filecount variable. More... | |
Static Private Attributes | |
static int | filecount = 0 |
Used to generate a unique filename. More... | |
A high level class to use system pipes.
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.
Pipe::Pipe | ( | ) |
Constructor.
Definition at line 42 of file Readers.cc.
Pipe::~Pipe | ( | ) |
Destructor.
Definition at line 51 of file Readers.cc.
void Pipe::closeRead | ( | ) |
Close the file descriptor associated with the consumer side of the pipe.
Definition at line 172 of file Readers.cc.
void Pipe::closeWrite | ( | ) |
Close the file descriptor associated with the producer side of the pipe.
Definition at line 188 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 65 of file Readers.cc.
void Pipe::create | ( | ) |
Create an unnamed pipe, both threads will read/write using file descriptors.
Definition at line 75 of file Readers.cc.
string Pipe::createNamed | ( | ) |
Create a named pipe, that is a FIFO special file and returns the filename.
Definition at line 90 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 61 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 58 of file Readers.cc.
int Pipe::openRead | ( | ) |
Return the file descriptor for the consumer side of the pipe.
Definition at line 132 of file Readers.cc.
int Pipe::openWrite | ( | ) |
Return the file descriptor for the producer side of the pipe.
Definition at line 152 of file Readers.cc.
|
staticprivate |
|
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.
|
private |
|
private |
|
private |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|