views:

46

answers:

1

Hi, after hours of tracking mysterious one or two seconds long lasting "freeze" I finally found out that its ReaderWriterLock(-1). It is server app and the lock here is held for writing to client collection. Im not familiar with locking so I would like to ask if there is not any better/faster way? How about using lock object to lock add/read statements of collection? This freeze occurs very randomly but its very annoying since it causes lag to all connected clients. Thanks!

+2  A: 

Do you mean AcquireReaderLock(-1)/AcquireWriterLock(-1) or similar? That would suggest that the lock is contended - in which case, fix whichever code is holding the (write) lock at the time. If the delay is literally with the locking class, then maybe ReaderWriterLockSlim would have less overhead. lock would indeed be simpler still (hence cheaper), but doesn't allow the same granularity - it is essentially a mutex lock, so only one thread can play with the object. Contrast a reader/writer lock which allows one writer NAND multiple readers.

To be honest, it sounds as simple as "occasionally, some code will take a write lock (blocking other callers), and take a while to complete". Finding the offending code is a black art, but has little to do with the lock itself.

Marc Gravell