0

I have a common problem in C#, I know how can I make the data to be thread-safe but I don't know which method would be best for my case.

My application receives data from serial port ( 10 floats per sample and 100 Samples/s) and I'd like to store that data somehow (heres a question as well, what would be the best idea to keep for example last 2000 samples? a queue of structs? ) and I'd like to access that collection from another thread to draw charts and also another thread that would perform calculations over the received data in real-time.

Should I use a simple lock? or maybe a concurrent collecion? or other?;)

  • 1
    Are both threads only reading the data or is the second one writing or replacing something during the calculation? – LostAvatar Sep 02 '13 at 07:37
  • 1
    Take a look at ReaderWriterLockSlim as well (http://stackoverflow.com/questions/2116957/readerwriterlock-vs-lock) – Vladik Branevich Sep 02 '13 at 07:40
  • No, threads (other than the serial port thread) don't modify the collecion. – Andrzej Czerwoniec Sep 02 '13 at 07:53
  • 1
    Take a look at BlockCollection - http://msdn.microsoft.com/en-us/library/dd267312.aspx. Sounds like you have a Producer/Consumer scenario. – Daniel Kelley Sep 02 '13 at 08:07
  • Alright, I'll take a look at those concurrent collections than, they seem to be the most reliable ;D What about the container for my sample (10 floats)? should it be a struct, class or maybe an array? – Andrzej Czerwoniec Sep 02 '13 at 09:45
  • 1
    @AndrzejCzerwoniec the microsoft guidelines specifies that structs should be used only when they have an instance size smaller than 16 bytes. In your case the instance size will be 4*10=40 bytes, so maybe is better to use a class. [Reference](http://msdn.microsoft.com/en-us/library/ms229017.aspx) – Alberto Sep 02 '13 at 11:35
  • I see, thanks for the reference, I haven't seen that before ;) I'll be using classes than and those concurrent collection, thanks for help :) – Andrzej Czerwoniec Sep 02 '13 at 11:59

0 Answers0