View Single Post
  #5  
Old August 11th, 2010, 01:17 PM
ddv ddv is offline
Seer
 
Join Date: Jan 2008
Location: RUS
Posts: 32
Quote:
Originally Posted by mnsandler View Post
would you care to share some sample code of the thread safe code and queueing mechanism you mentioned
ok.
1. All Queue's (gTransmitMsgQueue, gReceivedInsteonRaw, gReceivedInsteonText) that you use must be SynchronizedQueue. Because only SynchronizedQueue use lock for Dequeue/Enqueue. You use SynchronizedQueue only in TransmitMsgThread() but all other methods (TransmitGroupCommand, TransmitDeviceCommand and etc) use same queue (gTransmitMsgQueue) without lock.

2. Use AutoResetEvent instead of Thread.Sleep(100). For each queue declare AutoResetEvent like "AutoResetEvent gTransmitMsgQueueEvent = new AutoResetEvent(false)". Add gTransmitMsgQueueEvent.Set() after each Enqueue for this queue. Add same for each queue in ShutdownPlugin method just after "gShutdown = true" to unlock threads. Replace Thread.Sleep(100) to gTransmitMsgQueueEvent.WaitOne(100) in TransmitMsgThread(). In other methods use proper event name for each queue.

3. ResourceManager is absolutly not thread safe (several threads can simultaneously enter to any method and simultaneously check that it is FREE on same resource). Just use array of ReaderWriterLock in ResourceManager. It exactly what you want to implement (AcquireReaderLock for locks and AcquireWriterLock for exclusive locks).

To increase performance you have to change transmit logic. Now you transmit the data and wait result in same method in same thread. It should be divided. One thread should only transmit messages and ANOTHER thread should wait and process results for each transmission. It look like transactions...One thread start transaction and another thread process it result or retransmit it if no results received. Only need to provide when the same device has several transactions that do not start new until old will be done.


Best Regards,
Dmitry

Last edited by ddv; August 11th, 2010 at 02:05 PM.
Reply With Quote