License:
BSD style: see license.txt
Version:
Jun 2008: Initial release
author:
schveiguy
$(DDOC_MODULE_MEMBERS
class
ThreadConduit
: tango.io.device.Conduit.Conduit;
Conduit to support a data stream between 2 threads. One creates a
ThreadConduit
, then uses the OutputStream and the InputStream from it to
communicate. All traffic is automatically synchronized, so one just uses
the streams like they were normal device streams.
It works by maintaining a circular buffer, where data is written to, and
read from, in a FIFO fashion.
auto tc = new ThreadConduit;
void outFunc()
{
Stdout.copy(tc.input);
}
auto t = new Thread(&outFunc);
t.start();
tc.output.write("hello, thread!");
tc.close();
t.join();
- this(uint bufferSize = 1024 * 16);
- Create a new ThreadConduit with the given buffer size.
Params:
| uint bufferSize |
the size to allocate the buffer. |
- uint
bufferSize
();
- Implements IConduit.
bufferSize
Returns the appropriate buffer size that should be used to buffer the
ThreadConduit. Note that this is simply the buffer size passed in, and
since all the ThreadConduit data is in memory, buffering doesn't make
much sense.
- char[]
toString
();
- Implements IConduit.
toString
Returns "<thread conduit>"
- bool
isAlive
();
- Returns true if there is data left to be read, and the write end isn't
closed.
- uint
remaining
();
- Return the number of bytes
remaining
to be read in the circular buffer
- uint
writable
();
- Return the number of bytes that can be written to the circular buffer
Note that we leave 1 byte for a marker to know whether the read pointer
is ahead or behind the write pointer.
- void
detach
();
- Close the write end of the conduit. Writing to the conduit after it is
closed will return Eof.
The read end is not closed until the buffer is empty.
- uint
read
(void[] dst);
- Implements InputStream.
read
Read from the conduit into a target array. The provided dst will be
populated with content from the stream.
Returns the number of bytes
read
, which may be less than requested in
dst. Eof is returned whenever an end-of-flow condition arises.
- ThreadConduit
clear
();
- Implements InputStream.
clear
()
Clear any buffered content
- uint
write
(void[] src);
- Implements OutputStream.
write
Write to stream from a source array. The provided src content will be
written to the stream.
Returns the number of bytes written from src, which may be less than
the quantity provided. Eof is returned when an end-of-flow condition
arises.
|