Grofit
September 21st, 2009, 02:56 AM
Hey,
Im making some existing code thread safe, while trying to keep it high performance. Now im just putting lock(m_TheadLock){...} round all of the shared variable alterations/reads.
There are alot of sections where i could either put locks round each statement, which are things such as doing a collision detection check or adding/removing things from trees. This sounds best to me as this way you would be able to allow any other threads to keep taking actions in the small gaps between each statement, although this would incur more cycles im guessing as you are having to lock alot more frequently, also in some cases i need to create temporary variables to store the result from within the locks which means im allocating a small amount more memory (99% of the time its a simple value type so it shouldnt matter much).
The other route is to lock up entire sections that do alot of actions on volitile objects, so it would be one big lock, which would require me to create fewer temporary variables and make less lock calls, but the active thread would not release the area for other threads until it was complete...
Im currently going to the first method, but if this is the wrong way to go about it i would rather know sooner than later...
Any feedback would be great!
Im making some existing code thread safe, while trying to keep it high performance. Now im just putting lock(m_TheadLock){...} round all of the shared variable alterations/reads.
There are alot of sections where i could either put locks round each statement, which are things such as doing a collision detection check or adding/removing things from trees. This sounds best to me as this way you would be able to allow any other threads to keep taking actions in the small gaps between each statement, although this would incur more cycles im guessing as you are having to lock alot more frequently, also in some cases i need to create temporary variables to store the result from within the locks which means im allocating a small amount more memory (99% of the time its a simple value type so it shouldnt matter much).
The other route is to lock up entire sections that do alot of actions on volitile objects, so it would be one big lock, which would require me to create fewer temporary variables and make less lock calls, but the active thread would not release the area for other threads until it was complete...
Im currently going to the first method, but if this is the wrong way to go about it i would rather know sooner than later...
Any feedback would be great!