Closed shindanai-midas closed 2 years ago
@shindanai-midas the mutex is unnecessary here in the GetIterator function. You can generate as many iterators as you want and they are not shared among each other, so mutex here is irrelevant. A new iterator is generated each time you call the Iterator() function and each iterator is independent of one another. An iterator is simply a pointer to the underlying data structure to provide some traversal mechanism.
So I am not sure what it is that you would like to achieve here? To protect the underlying data structure or protect access to the same iterator by multiple go routines?
@emirpasic I want to protect access to the iterator by multiple goroutines. At first I'm not sure that if I get multiple iterators from the same tree will affected each other while they're traversing concurrently, especially calling Next() function. And as you said
"A new iterator is generated each time you call the Iterator() function and each iterator is independent of one another"
so I knew that it's ok to use iterators concurrently even if it came from the same tree.
Thank you
My code have to process with the iterator concurrently
Example
Here is my question
Would it share the same state of the Iterator? Would it make iterator wrong running?