LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading Help/Suggestion

I have created a program which uses 2 threads.

 

Thread 1

Reads commands from buffer

Sends commands out to device1

Displays commands in a table in GUI

 

Thread 2

Reads commands from device2

Manipulates data

Displays data in a table in GUI

Saves data to excel spreadsheet

 

My question is this:  Should I offload all the data displaying and saving to a Thread Safe Queue ?

If so, can this still be done if the data is of different types ( eg, int, long, char, long long, double, ect ).

 

I have read all the examples and having a hard time understanding how to use a safe queue with many different data types.

 

Thanks

Diego 

 

0 Kudos
Message 1 of 5
(3,800 Views)

it depends on your requirements: if your applicaiton can handle the load without problem, you can let it this way. keep in mind that having another thread will only allow for a smoother run, but if your application cannot handle the average data rate, it will not handle it either with another thread. the thread safe queue will only provide a buffer to compensate for data burst.

 

the thread safe queue is not designed to store different data types in the same queue. you can however use the variant idiom: create a structure which can handle multiple data type and store this type of data into the queue. here is an example:

 

typedef enum

{

    variant_type_int,

    variant_type_float,

    variant_type_double,

    variant_type_char

} VARIANT_TYPE;

 

typedef struct

{

    VARIANT_TYPE type;

    union

    {

        int int_value;

        float float_value;

        double double_value;

        char char_value;

        /* add any data type you want, add a corresponding value to the enum above */

    };

} VARIANT;

 

use this type to store data into the queue. don't forget to set the "type" member to the value which correspond to the type you store into the variant. now you have a queue which can handle multiple data types simultaneously.

0 Kudos
Message 2 of 5
(3,788 Views)

Hi Diego,

 

Using thread safe queues is a wise choice. Use them to transfer data between your threads.

Think of them as pipes between threads. One thread can push data from one end, the other one pulls them out.

 

Since your are putting into these pipes the incoming data of a communication bus, your data is probably formatted (encoded) to have some meaning in the real world.

So it is a matter of interpretation how the data is handled and displayed. You can put into the pipe a 4-byte integer, followed by an 8-byte double, followed by a single byte unsigned char.

They can all live in the queue until another thread pulls them out.

 

Then it is the problem of that thread how to handle (display on the GUI in your case) these bytes.

You may convert them to analog values (using two bytes for each value, for instance) or just list them as hex digits. It is upto you...

 

Try this;

[ Thread_1: Read from buffer, send to device_1, put data to queue_1 ] -------> [ GUI_Thread: Read from queue_1 and queue_2, display on the panel ] <------- [ Thread_2: Read from device_2, manipulate, put data to queue_2, save to Excel ]

 

I hope I could make the idea clear

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 5
(3,778 Views)

Thank you both for your response.

 

I do have the application, as stated in my original post, working with no major issues.  Regardless, I am always looking for a way to streamline and or clean up my messy code, hence my question.

 

However, I am a little confused.  If I understand your reponses correctly, can I or cant I pass different types of data through to the safe thread ?

 

Thanks

Diego

 

0 Kudos
Message 4 of 5
(3,753 Views)

Hi Diego,

 

All the native C data types are values represented by consecutive (1, 2, 4 or 😎 bytes of data in the memory.

Since your code is both writing to and reading from the queue, it is upto you how to interpret the data you read from it.

 

If you know the order of writing them to the queue, you can read them safely.

You can divide a double variable's binary representation into two 4-byte parts, each acting as an integer.

You can put them into the queue (if initially you configured to queue to store 4-byte items) as 2 consecutive parts.

 

Then other part of your application can read the two 4-bytes in a single read operation and you can construct the double value again.

The critical part is knowing which value is staying in which order in the queue.

 

Hope this helps, 

S. Eren BALCI
IMESTEK
0 Kudos
Message 5 of 5
(3,741 Views)