08-01-2013 12:55 PM
I have 2 parallel while loops.
There is a variable(runs from 0-50) in one that I would like to access simultaneously in the other loop...I tried createing a global variable but it seems that it is only reading the very first value(0). I know there might possibly be a race condition, but shouldnt the variable be updated in the second loop continously anyways?
I have read this thread already but still couldn't solve the problem:
Thanks!
08-01-2013 01:21 PM
Use Queue/Notifier Functions to Comunicate/share data among the loops.Variables are slow and unpredictable :[
Can u show some code?
08-01-2013 01:30 PM
so you mean global variables won't work?
Actually my code is too big to post, since this a minor part of a large program,
But basically I have the producer/consumer pattern, where I enqueue the elements in the producer and dequeue them in the consumer.
I dequeue 8 times, and build an array of 1x8. Then I want to insert this array into a bigger 2D array, using replace subset. The counter (which is my global variable) is created in the producer loop (0-50).
The new 2D array will be 50x8, so the index for the replace array subset would be the counter (0-50).
I want to store each 1D array into a separate row of the 2D array, since I will need all the values later.
Hope this is clear...If not I can make a sample code of what I mean.
08-01-2013 01:51 PM
You should be careful when you maintain state information across loops. In addition, global variables are not the best method for conveying information. They can easily lead to race conditions. They also highly couple your code making it more difficult to ever reuse portions of it. Generally state information, such as your array index, should be maintained in one process. Other tasks that are posting to a queue should simply post the data. When tasks are tightly coupled debugging can become much harder. If you are getting some count in one porcess and that will state where the data should be placed than instead of having a global or local variable change your message content to a cluster that includes both the data and the index. I generally prefer a messaging format that is a cluster of a message type, either a string or an ENUM, and a variant. This messaging construct can pass basically any type of data. Only the message producer and message consumer need to worry about the message content itself. This consttruct is very flexible and easy to extend.
08-01-2013 02:20 PM
Producer Loop means "Generate" usable Data.
Consumer Loop means "do something useful" with that Data. Sounds Poetic....
Therefore.... (If i understand correctly ur situation...)
Why u not dequeue the whole 1D Array instead of building that element by element in consumer loop?
Then u can use replace subset directly to 2D Array... No need for that "Counter".
OR
U can create an Large 1D Array with all elements, and after finish the "dequeue process" ,u can reshape it to 2D Array (U know the final size)
OR
U can put that counter in Consumer Loop with Shift Register and Increment that at 8th iteration (What means One Array already dequeued).
Choose one.
08-01-2013 02:50 PM
why dont you pass a reference?
08-01-2013 05:30 PM - edited 08-01-2013 05:34 PM
@apok wrote:
why dont you pass a reference?
That isn't much better than using a global/local. Actually, it can be worse since it will force the task to run in the UI thread. I try to avoid using refernces solely for the purpose of passing data around.I try to use them only for VIs that are updating the UI itself and I will have separate tasks specifically for handling the UI. Processing code will be separate tasks which are UI agnostic.
08-01-2013 05:44 PM
@developer001 wrote:
I dequeue 8 times, and build an array of 1x8. Then I want to insert this array into a bigger 2D array, using replace subset. The counter (which is my global variable) is created in the producer loop (0-50).
The new 2D array will be 50x8, so the index for the replace array subset would be the counter (0-50).
I want to store each 1D array into a separate row of the 2D array, since I will need all the values later.
Hope this is clear...If not I can make a sample code of what I mean.
Replace array dont increase array size (build or insert does), so unless you've initialized an array alread you'll stay at 0 items, which'd give the behaviour you describe.
/Y
08-02-2013 11:36 AM
Yes I have already initialized the array to be 50x8.
08-02-2013 11:42 AM