LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading problem

Solved!
Go to solution

Good evening,

 

I need help finding a solution to a Thread problem.

I'll try to explain the situation.

 

I have a main thread that keeps looping through a set of data (e.g. an array of strings).

At a certain point I have to perform some very slow operations and, to avoid slowing down the main thread, the latter launches a child thread which performs the operations and then closes.

This child thread needs the string array declared in the main thread but the values in this array may vary in the main thread.

But since CmtScheduleThreadPoolFunction passes the address of the variable (string array in my case) the value will also get changed in the secondary thread. Which I don't want.

 

Can you help me find a solution?

 

Thank you

0 Kudos
Message 1 of 4
(1,339 Views)

I see no way  but passing a copy of the string (array) to the secondary thread. This could be done with a Thread Safe Queue



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 4
(1,319 Views)

Thank you for your response Roberto.

 

I've neve used Thread Safe Queue. 

So I have a couple of question.

To read and write from the queue is it better to use CmtRead/WriteTSQData or CmtGetTSQRead/WritePtr?

 

-Is it correct to allocate the memory of the elements to be added to the queue in the main thread and perform the free in the secondary thread that reads the elements from the queue?

 

Thank you for your precious help

 

Ciao

Mattia 😉

0 Kudos
Message 3 of 4
(1,290 Views)
Solution
Accepted by Mattia.G

To read and write from the queue is it better to use CmtRead/WriteTSQData or CmtGetTSQRead/WritePtr?

I never used to read/write from the pointers because of the caveats that you can read in the documentation. As far as I can understand, if you fall in the case described there those methods should be equivalent.

 


-Is it correct to allocate the memory of the elements to be added to the queue in the main thread and perform the free in the secondary thread that reads the elements from the queue?

The buffers to use on two sides of the queue must be different: using the same buffer frustrates the queue paradigm which is to guarantee thread separation. It would be as if you connect both sides of an optocoupler with the same wire!



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?
Message 4 of 4
(1,234 Views)