LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Thread safe queue (TSQ) help

Hello,
 
I have two threads running in my application:
 
1. Time crtical thread. This thread reads 4 channel DAQ data from DAQ board, 1 data from a series port, and calculates time interval from a timer. The whole process will take about 0.01 s (100 Hz) and this thread will collect about 60000 data points.
 
2. Real-time data analysis and display thread. This thread will save, analyze, and display the data collected from thread 1 in real time. This thread will also process system events. One of the time consuming process is displaying two sets of data in a graph.
 
Since thread 1 is much faster than thread 2, in order to avoid queue overflowing. I am going to use the following TSQ structure:
1. Create a thread queue with a length of 60000 with a thread queue read callback function which will read the queue when the item number in the queue is 600.
2. In thread 1, I am going to write the queue every 60 points.
 
Will my algorithm work correctly? Or anyone has a better solution for my case? Thanks
 
0 Kudos
Message 1 of 5
(3,840 Views)
Anybody can help me? thanks.
0 Kudos
Message 2 of 5
(3,818 Views)
Hi,

It seems like your general logic is sound.  Your overall application architecture sounds good as well.  This scenario is the perfect case for a queue.  There is a CVI example that is somewhat similar to your application.  It is acquiring data and sending it out across the network through TCP/IP.  There is one thread for sending the information across the network and another thread for acquiring the data, and the two threads communicate via a TSQ.  You can get to the example by going to the CVI example finder and searching for queues.  Hope this helps.
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 3 of 5
(3,816 Views)

Thanks Pat. If the algorithm is correct, I have questions about the queue write and read:

    CmtInstallTSQCallback (tsqHandle, EVENT_TSQ_ITEMS_IN_QUEUE,
                                            600, QueueCallback, NULL,
                                            CmtGetCurrentThreadID(), &plotDataCallbackID);

   CmtWriteTSQData (tsqHandle, writeBuffer, 60, 0, NULL);

   CmtReadTSQData (tsqHandle, readBuffer, 600, TSQ_INFINITE_TIMEOUT, 0);

 The above functions are what I used for my queue. I am wondering when my QueueCallback will get called? According to the explaination of EVENT_TSQ_ITEMS_IN_QUEUE, the callback will be called when a. items are written to the queue. and b. number of items exceeds 600. However, as I debug my code, I noticed the QueueCallback will only be called when the queue is full (60000). What happened?

Thanks.

0 Kudos
Message 4 of 5
(3,813 Views)
Hi,

Your assumptions are correct.  The callback should be called as soon as you have a write in which the number of items in the queue meets or exceeds 600.  I wrote a small example here using the same functions, and the queue functioned as expected.  I would recommend starting with simulated data to see if you can get your queue to work properly.  If you have a stripped down version of your project, you could post it here for us to take a look at.
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 5 of 5
(3,796 Views)