I have a list, to which I write once in every update Loop and then I spawn some tasks which create read that List and create a duplicate to manipulate that data.
More or less it's List.ToList()
call to create a new list instead of using it by reference. Each task manipulates that data in it's own and produces it's results.
Currently I am using a traditional lock in the form of
lock(LockObject){
//write stuff
}
when writing and
lock(LockObject){
localList=List.ToList();
}
when reading.
Would using the ReaderWriterLockSlim
object instead of the way I am doing it now provide me with any benefits speed-wise? Also is the ToList()
method considered a read in this case?
Clarification Edit
- The readers create a copy because they manipulate the data differently, each one of them removes or adds data to the List on their own way. (some might add previous data they still need while others remove data they do not need. Also the update loop occurs once every few seconds so it is common.
- Sometimes if the data input is really fast an update/write loop might start before all the readers have completed their processing (but if they get the updated data instead is no problem for me).
- Basically the readers are there for presenting current data to others. So if data is changed before they even start presenting them that's ok. As long as they do not try to process data while they are being update (which would probably throw an exception)