LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Rading cluster elements

I pass a reference of a cluster to a VI. This VI runs allone, so not in the main loop of the calling VI. In the main loop of the calling VI I write values in the cluster. In the SUB-VI I access data over the reference of the cluster (so like passing a pointer to a function in C). In the Sub-VI some elements (there values) of the cluster, which are not written in the main-VI, are changed. How do I access to this values in my main loop. Unbundling does not help. When I write the values to the cluster I use the bundle-function. Because my main loop runs asynchroun to the Sub-VI it seems, that when I write the values in the cluster, also the not written values are set to 0, and I read 0 when I unbundle the cluster. Is it possible that these values,
which are not written in the Main-VI keeps there value, or exists an other possibility ?
0 Kudos
Message 1 of 8
(3,357 Views)
Without looking at your code I cannot tell you much, But from your descrition I have a couple of questions.

1. Does your subvi have a loop on the inside of it to keep it running: If not then the vi will run only one time and stop.

2. do you have data flow dependency to the subvi: If you do this could be the problem.

3. This would be a great place to use ques because you can pass data using them.

I know i did not help much but maybe I gave you some iseas.

Joe



Joe.
"NOTHING IS EVER EASY"
0 Kudos
Message 2 of 8
(3,357 Views)
I read this as a race condition.

It sounds like the user is attempting to use the cluster as a global.

But without some code to look at, I am only guessing.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 3 of 8
(3,357 Views)
Hi Joe,

1. It has aa loop running, so it also does what I want (collecting data all the time)

2. No, I just start at the beginning of my Main-VI the Sub-Vi, passing to it the reference of a cluster. the data of the cluster, which must go to the sub-VI is manipulated in the main-VI. The sub VI also calculates some parts and puts the results in som members of the cluster (which is passed by reference). In the main VI I would like access this maipulated data, but only 0 is in.

3. What is a que in LabView ? (I'm a beginner, because usually I program in CVI but this project must be done in LabView, thats the reason why I think like a C-programer)

Regards Heinz
0 Kudos
Message 4 of 8
(3,357 Views)
"...I think like a C-programer..."

This can be cured with a large dose of vitamin G on a regular basis!

Please post some code that show what you are trying to do. We provide better answers when we have visual aides to work with.

Lacking example code I will proceed to guess.

It sounds like you have a cluster control/indicator on your top level VI that gets updated from ore than one place.

You are updating it in the top level VI and the sub.

"This is bad." (Mr. Macky S. Park)

In LV and C it is not a good idea to update the same data structure from more thatn one place in your code without taking special precautions to ensure that the shared data structres are not corupted due to subtle timing issues.

STILL GUESSING!

At some point your sub VI and top level VI attempt to modify part of the cluster. If one peice of code reads the cluster, and then bundles in a new value, the other part of the code could be reading the previous contents of the cluster before the first completed. So the second reader get a copy of the old data to do its work,
BUT meanwhile the the first chunk of code now writes its updated cluster values.

The other peice of code that is using the old data now does its bundling based on the stale data and it does its update.

Bingo! Race condition.

In C you could prevent this conditon by using a protected section or mutexes to ensure that the two processes did not step on each other.

Both peices of code would first acquire a mutex before updating the shared data structure. When they were done with the update, they would release the mutex to allow others to work with the shared data.

If I got my C version goofed up, please forgive me. I gave up C years ago.

In LV alot of the low level "computer specific" work required in C is done automatically in the background. While this allows developers to concentrate on the application rather than "taming the beast",
BUT,
Sometimes the behind the scenes work can mask some issue.

One of these areas that require some special thought in LV is,
"How do I share data bewteen multiple threads?"

You can get away with using a front panel control (like I am guessing you are doing) provided there is only one writer or the control. The number of reders is unlimited. (Note: I do not encourage the practice and if one of my rookies did this, I would have to talk to them. FP controls should be thought of as as method of getting info from a user or telling them something, they are not data storage elements!).

There a many methods of sharing data between threads in LV. Above, queues were suggested, and my personal favorite is the "LV2 Global" (aka functional global, action engine, Uninitialized Shift Register, USR, ...).

THe queues functions can be found on the
Advanced>>>Syncronization >>> Queue operations pallette.

A queue can be thought of as a pipe. You can stuff wads of data in one end and the pop out the other end in the same oreder they we pushed in. I invite to explore that palette and post seperate follow-up questions as they occur to you.

Earlier I said FP controls are permisable if there is only one writer and multiple readers.

Queues genearlly adapt themselves well when you have multiple writers and a single reader.

What if you want to read and write from multiple locations?

That is where the LV2 comes in handy.

I have attached an example of a LV2 Global.

This construct is rather amazing. It takes advantage of some of LV's behind the scenes work.

1) Becuase the VI is non-Re-entrant, no two threads can execute the LV2 at the same time. This is accomplished by LV using mutexes (behind the scenes) to ensure no two threads are executing it at the same time.

2) A shift register is used to stor the data. Th shift register acts like a Static local in that it persists from call to call.

OK, I'm done rambling for this AM.

Have fun,

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 5 of 8
(3,357 Views)
Hi,

now I have a small example what I want to do. Maybe you can help me (in between I solved the problem in an other way). Also I dont understand, if I have a strict typedef control, how I define the diemnsion of an array, which is for example part of a cluster in this control. I thin, thats also the main problem for me, because of that, it seems the auto-loop doesnt work correctly. You can see this in my example in the sub-VI, where I have a loop to change values in this array, but nothing happens. In the comment of the test-VI below the loop you can see, what I want to do, explained with a short C-program.

Regards Heinz
Download All
0 Kudos
Message 6 of 8
(3,357 Views)
Hi Heinz,

I have modified youd sub-VI to do what I think you want.

I do not think you will be happy with the results.

This VI is now an excellent example of a race condition!

If you;
1) Put the sub-VI in execution high-lighting,
2) Run the main VI
3) Watch the sub-VI until you see the cluster has been read,
4) Increment the "numeric" control on the front panel of your main
5) Watch the sub-VI write the old cluster value over top of what you set it too.....

You can witness the race condition.

I have also re-written the top level VI to show you the more traditional method for passing data to a sub-VI and using the results to update an front panel object. It also suffers from the same race con
dition.

This can be avoided using the LV2 construct.

I'll post another comment with the LV2 approach illustrated.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Download All
0 Kudos
Message 7 of 8
(3,357 Views)
Watch both of these sub-VI's in execution highlighting.

I hope a light will come on once you watch these VI's run.

I have added a seperat control to specify the numeric value you use in the cluster. I need to elliminat the race condition of having two writters (GUI plus code).

The data is now stored in the shift register of the sub-VI. Again the shift register acts like a static local. The SR is in the scope of the sub-VI only. The values written to the SR persist from call to call.

Once you understand the attached examples you will be developing multithreaded app in no time.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Download All
0 Kudos
Message 8 of 8
(3,357 Views)