M
Mark Stijnman
I have a question about forward iterators and what one should do or not
do with them. I'm planning on writing a container that, when all boils
down to it, stores a sequence of strings. I want threads to be able to
read this sequence from start to end, and when they reach the end, wait
until new data is added to the end. If so, it should pick up reading
where it left off. The question is, is it valid and moral to do
something like this from a reader thread (synchronization code is left
out here for brevity/clarity):
MyContainer::const_iterator ReadIter; // a forward iterator
MyContainer ReadThis; // a container
for (ReadIter = ReadThis.begin(); ReadIter!=ReadThis.end(); ++ReadIter)
cout << *ReadIter << endl;
// at the end of the loop ReadIter is the past-the-end iterator
returned by the last end() call.
// Thread waits for ReadThis to have one more more items added.
WaitForItemsAddedSignal(...);
// Here's the catch: assume ReadIter is now no longer the past-the-end
iterator, but is now
// referencing the next unread string, so we can just pick up where we
left off like so:
for ( ; ReadIter!=ReadThis.end(); ++ReadIter)
cout << *ReadIter << endl;
So, a Forward iterator that used to store the past-the-end iterator,
returned from a previous call to end(), now suddenly references an
actual item. It is clearly possible to implement something like this,
but is it a good idea? It might be somewhat confusing: It assumes
something about the iterator (or the container?) that 'normal' forward
iterators don't guarantee. Of course I could specificly mention this in
the description of MyContainer that I give this guarantee, but should I
go this route to begin with? Or should I use another way of keeping
track of the "last read" item?
Thanks for any insights,
regards Mark
do with them. I'm planning on writing a container that, when all boils
down to it, stores a sequence of strings. I want threads to be able to
read this sequence from start to end, and when they reach the end, wait
until new data is added to the end. If so, it should pick up reading
where it left off. The question is, is it valid and moral to do
something like this from a reader thread (synchronization code is left
out here for brevity/clarity):
MyContainer::const_iterator ReadIter; // a forward iterator
MyContainer ReadThis; // a container
for (ReadIter = ReadThis.begin(); ReadIter!=ReadThis.end(); ++ReadIter)
cout << *ReadIter << endl;
// at the end of the loop ReadIter is the past-the-end iterator
returned by the last end() call.
// Thread waits for ReadThis to have one more more items added.
WaitForItemsAddedSignal(...);
// Here's the catch: assume ReadIter is now no longer the past-the-end
iterator, but is now
// referencing the next unread string, so we can just pick up where we
left off like so:
for ( ; ReadIter!=ReadThis.end(); ++ReadIter)
cout << *ReadIter << endl;
So, a Forward iterator that used to store the past-the-end iterator,
returned from a previous call to end(), now suddenly references an
actual item. It is clearly possible to implement something like this,
but is it a good idea? It might be somewhat confusing: It assumes
something about the iterator (or the container?) that 'normal' forward
iterators don't guarantee. Of course I could specificly mention this in
the description of MyContainer that I give this guarantee, but should I
go this route to begin with? Or should I use another way of keeping
track of the "last read" item?
Thanks for any insights,
regards Mark