LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Structure protection in thread with the safe queue

Hi,

I search some information about multithreading with CVI 6, because I
must program an acquisition system with serial port.
I receive data from serial port interupt (InstallComCallback). I save
data in a buffer (array) which has a structure with 11 (double)
variables and after I go out of the interrupt.
In other thread, I do calculation with the data of the buffer, I read
only.
I would like to know if I must protect my buffer?
Thanks.

Mariano
0 Kudos
Message 1 of 6
(3,780 Views)
Mariano,

The architecture that you have is commonly know as a producer/consumer application. There is one thread generating data and another thread reading it. The main problem with this setup is overwriting or overreading data; that is, the producer and consumers loop have different rates.

the main problem that you can find is that your reading thread reads the same data more than once. Or that your writing thread overwrites data that has not been read jet. If you can live with these type of errors you don't need buffer protection.

If you need full data integrity and thread rate sinchronization you would need to use a thread safe queue. This way no data will be lost and you'll have correct sinchronization. you can find some examples at C:\Program Files\Na
tional Instruments\CVI70\samples\utility\Threading\ThreadSafeQueue. There is an example on no data loss for buffers that is a good place to start.

I hope this helps, please let me know if you have any further questions.

Regards.

Juan Carlos
N.I.
0 Kudos
Message 2 of 6
(3,780 Views)
Hello,

Thank you for your help. I understand what you mean. I write my
program consequently. In my reading thread, I wait for data by
checking the input buffer index. Which increments as soon as the data
arrived.
Then, I think that is not important which I protect the buffer, but I
would like to know if I'm right.

Best regards.

Mariano.


JuanCarlos wrote in message news:<506500000005000000F6C70100-1079395200000@exchange.ni.com>...
> Mariano,
>
> The architecture that you have is commonly know as a producer/consumer
> application. There is one thread generating data and another thread
> reading it. The main problem with this setup is overwriting or
> overreading data; that is, the producer and consumers loop have
> different rates.
>
> the mai
n problem that you can find is that your reading thread reads
> the same data more than once. Or that your writing thread overwrites
> data that has not been read jet. If you can live with these type of
> errors you don't need buffer protection.
>
> If you need full data integrity and thread rate sinchronization you
> would need to use a thread safe queue. This way no data will be lost
> and you'll have correct sinchronization. you can find some examples at
> C:\Program Files\National
> Instruments\CVI70\samples\utility\Threading\ThreadSafeQueue. There is
> an example on no data loss for buffers that is a good place to start.
>
> I hope this helps, please let me know if you have any further
> questions.
>
> Regards.
>
> Juan Carlos
> N.I.
0 Kudos
Message 3 of 6
(3,780 Views)
Hi,

Seems to me that you should be ok. The only case where I could see some problems is if you check the index value and decide to read the data; while you are reding the data the writing thread updates the value of that index, then your reading thread sets the index to zero withour reading the new incoming data.

This is not a very likely scenarion, however if data integroty is critical you may want to make that inder into a thread safe variable.

Regards,

Juan Carlos
0 Kudos
Message 4 of 6
(3,780 Views)
Hello,

I would like to save my buffer, but I don't know if we can do that
with a buffer structure like that:

struct buffer_erreur
{
double X_pos;
double Y_pos;
double Z_pos;
double X_acce;
double Y_acce;
double Z_acce;
double X_current;
double Y_current;
double Z_current;
double Vibra_1;
double Vibra_2;
} buffer_circ[100];

I don't know if I can use the thread safe queue for my structure? If
it's possible how I do that?

Regards,

Mariano


JuanCarlos wrote in message news:<5065000000050000008CC90100-1079395200000@exchange.ni.com>...
> Hi,
>
> Seems to me that you should be ok. The only case where I could see
> some problems is if you check the index value and decide to read the
>
data; while you are reding the data the writing thread updates the
> value of that index, then your reading thread sets the index to zero
> withour reading the new incoming data.
>
> This is not a very likely scenarion, however if data integroty is
> critical you may want to make that inder into a thread safe variable.
>
> Regards,
>
> Juan Carlos
0 Kudos
Message 5 of 6
(3,780 Views)
The example Located at C:\Program Files\National Instruments\CVI70\samples\utility\Threading\ThreadSafeQueue\DirectPtrAccess\ demonstrates how to use pointers with thread safe queues. You can cast any kind of pointer there to sand custom data types.

I hope thi shelps.

Juan Carlos
0 Kudos
Message 6 of 6
(3,780 Views)