LabWindows/CVI

Content Type
Programming Language
Current manual

Thread Safe Queue Class Help

Thread Safe Queue Class Help

This class contains functions you can use to safely pass data between two threads. The following example shows a situation in which a thread safe queuethread safe queue is useful:

  • You use one thread in your application to acquire data.
  • You use another thread to display the data and handle the user interface.
  • You use a thread safe queue to pass the data from the thread that acquires the data to the thread that displays the data. Specifically, the thread that acquires the data puts the data into your thread safe queue, and the thread that displays the data reads the data from that queue.

A thread safe queue keeps track of the amount of data available for reading and the amount of space available for writing. Only one thread can read from a thread safe queue at a single time and only one thread can write to the queue at a single time. However, a thread can read from the queue while another thread writes to the queue.

You can use a thread safe queue in two ways. You can continuously read and write, or you can read and write in response to events. When you use continuous access, the writing thread calls a function to add data to the queue. The function returns when all the data has been written to the thread safe queue or a specified timeout occurs. The thread that reads data accesses the thread safe queue in a similar way.

You can register read and write callbacks that are called when data is available in the queue or space exists in the queue for you to write more data. When data becomes available, the queue calls the read callback. When space in the queue becomes available, the queue calls the write callback. You do not need to use the thread safe queue in the same way for both the thread that reads data and the thread that writes data. For example, the thread in which you are doing data acquisition might write data to the queue continuously while the thread handling the user interface registers a callback with the thread safe queue and reads data when a certain amount is in the queue.

You can access the data in a thread safe queue in two ways. You can pass buffers to functions that copy thread safe queue data, or you can use pointers to access the thread safe queue's internal buffer directly. CmtReadTSQData copies data from the thread safe queue into a buffer that you specify. CmtWriteTSQData copies data from a buffer that you specify into the thread safe queue.

Alternatively, CmtGetTSQReadPtr and CmtGetTSQWritePtr return pointers that point directly to the thread safe queue's internal buffer. While this approach can potentially provide better performance, it is more complex to use.

Library: Utility Library

Was this information helpful?