I have this situation:
I have a class which keeps track of an array of pointers. I built a custom iterator which loops through this array.
My problem is on how to make it threadsafe, especially while incrementing/decrementing?
Here is a draft of the relevant parts of what I have:
typedef fruit * iterator;
class fruits
{
private:
fruit ** flist;
int n; //keeps track of position in flist
int count; //number of fruits
public:
iterator begin() {n=0; return fruit[n];}
iterator end() {n=count; return fruit[n];}
iterator operator++ ()
{
return fruit[++n];
}
}
The problem i see is that if two parts of the program create an iterator things won't work. How does C++ STL deal with it?
UPDATE: I have found the error of my ways. The iterator should keep track of where it is by itself. For that I created an iterator class embedded in my main class. Life is now good.