LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

[ETA: posted in wrong forum, sorry! will repost in relevant forum] reading samples: critical section atomicity?

Solved!
Go to solution
Hello all,

I have the following situation:
- one thread (T1) is constantly reading samples from a NI USB 9229 in a local buffer and copying them an array A (44100Hz)
- another thread (T2) is waiting for a command and subsequently copying a batch of 44100 samples from A to a local array variable and doing some computation with them
- array A is protected in a critical section (T1 and T2 enter the critical section when working with A directly and leave it afterwards)

How "atomic" is the reading of samples from the device in my array A? I.e., is it guaranteed that the critical section protection on A means that by the time T2 accesses A, T1 has finished writing 44100 samples in it?
Or do I have to register with DAQmxRegisterEveryNSamplesEvent() a callback which 1) reads 44100 samples, 2) notifies T2 that the batch is complete and available?

Thank you in advance!
Message Edited by acgrama on 02-08-2010 08:30 AM
0 Kudos
Message 1 of 3
(3,312 Views)
Solution
Accepted by topic author acgrama

As far as I can understand the situation, the answer should be 'yes': the array is protected until you leave the critical section.

 

But could I suggest an alternative, perhaps simpler approach?

If you configure a thread safe queue of 44100 elements which discards old elements, with T1 that fills it on top, T2 can freely read from the queue without disturbing T1, having available the most recent set of data in any moment.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 3
(3,308 Views)

Thank you for the kindness of answering my post even though it was in the wrong place 🙂

I am analyzing a couple of alternatives at the moment (the situation in my app is a bit more complex than I described here), but the thread-safe queue might be a good idea for at least part of my data structure requirements!

0 Kudos
Message 3 of 3
(3,284 Views)