Program a simple iterator?

Lets say, there is an array with data and several threads need to access it simultaneously just as easy as:
1
2
3
4
5
while( it != it.end() )
{
cout << *it << endl;
it++;
}

There is something for it in STL, but I feel like I don't want to look at it, before I can program myself. Maybe you have some useful link?
I am not sure the STL iterator is thread-safe. You'd want to put mutexes or locks in your code to be sure.
what do you mean simultaneously i mean it cant happen all at the same time right ... Zaita is right you need locks or at the very least some sort of passport system so you don't get some threads accessing it multiple times before others get to it right?
Multiple threads can trivially read an array safely if none of them are modifying it.

C-stlye arrays (mening the iterator is really a pointer) may be thread safe, if it's an array of "atomic" types, such as an array of ints.

STL containers are not threadsafe (unless you have library created just for that purpose), so you'll need some locking mechanism if you want multi-threaded access to a non-constant container.

And always remember that adding a new element to a vector can invalidate all iterators, whether you have one thread or many.
Ok. Lets talk about constant array: if multiple threads are reading it at the same time, they must maintain each its own index for array. The problem is: how to initiate that index and use for easy accesses? I mean, without messing with it locally where the while loop is; similar approach can be seen when some kind of iterators are used...
If its trivial, please post...
If you wanted multiple threads to traverse a const collection, you'd give each thread it's own iterator over the common const collection.

You do not what threads sharing an iterator.
Topic archived. No new replies allowed.