LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2 loops(threads) operate on one array

I have a array, it is on server, used to record the client's operation.

 

There are 2 loops on server to maintain this array.

 

Loop1:

1.When one client apply to operate on data A in database, it will add one record in array marked as 'On' with a timestamp also.

2.When one client apply to operate on data A in database, and A is already under operating by another client, it will still add one record in array, but marked at 'Wait'.

3.When one client apply to 'finish to operate on data A in database', it will delete the its record accordingly.

 

Loop2:

1. Any 'On' record exiting longer than 20s will be seen as timeout as client dead, and the 'On' record will be deleted.

2. When no more client ocuppy A and still has client 'Wait' for  A, it will send msg to trigger 1. in Loop 1.

 

But seems it need complex data interlock between 2 loop, or there will be errors.

 

Any suggestion?

 

0 Kudos
Message 1 of 4
(2,341 Views)

Hi turbot,

      I looked at your code and I don't think there's any quick fix - it would benefit from some real structural change.

 

Current Problems (not a complete list):

There's a "race condition" because the LockSpot array is being read then changed in two different places at the same time.

I think the loop that's removing old records and changing records (from Wait to On) will only update one record successfully - the last one it changes.

 

One (major-change) suggestion:

Manage the LockSpot array in a SubVI and have "Import Manager" (IM) call the SubVI - see attached example "LockSpot.vi".  The main immediate advantage is that this will prevent the two IM loops from using LockSpot at the same time.

 

Another recomended change

Change the LockSpot array from a 2D array of strings, to a 1D array of Spot records - think of the cluster as implementing the Spot-request abstraction.  Using a cluster allows the Timestamp to be kept as a timestamp type; you can also keep "Status" as a boolean - this will keep your code cleaner, cleaner is simpler and simpler is better!Smiley Very Happy 

 

Cheers!

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
Message 2 of 4
(2,305 Views)

I agree with tbd on all points!

 

What is an upper limit on records? I would probably keep the data inside an action engine subVI as a fixed size array, with a few shift registers keeping track of the number of valid elements, etc.

Message 3 of 4
(2,298 Views)

Thanks tbd,

 

All your suggestion are really precious. Now actually realize that function global is the way in LV to somehow interlock the data.

 

I searched from LV help and found semaphore is also available in LV. Just one more question, how does semaphore compared to function global. When it will be good to use semaphore, how to use it. Thanks. Since I saw less use of semaphore, maybe I am wrong.

0 Kudos
Message 4 of 4
(2,270 Views)