LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple While Loops. Multithreading

Hi everyone,

 

I have written a program which contains multiple parallel while loops (15 or more)  which seem to execute simultaneously. For the moment, the application works works perfectly as all the threads ( loops ) start at the same time bringing own cycle time and LV seems to work in parallel. The different while loops have to work constantly without being stopped at any time.

 

The problem is that the code is beginning to be a real monster because of the the large number of loops in just one frame and each becomes huge. I would like to work in different frames but obviously that is complicated since are infinite loops. On the other hand, I gues I should control the data flow that is becomeing a mess. None of the loops is master over the other and have to run autonomously with its own cycle time. And there are shared variables that could influence on the race condition.

 

I attached an example of a very simple app where you could imagine that each while loop would be so big.

 

Thank you very much for the help.

0 Kudos
Message 1 of 23
(11,137 Views)
Hi,

why do you need frames or sequences? Use subVIs when the VI becomes too big!

To sync loops you could employ notifiers or queues...
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 23
(11,115 Views)

Because the main frame is so huge (and is getting bigger) and is quite hard to anderstand the code because of the number of while loops.

 

I can´t use subVIs with infinite loops inside.

 

It´s more a debugging problem cause the app runs fine. 

 

With the sync loops (notifiers, queus, semaphores...), each loop must wait to start to run. I want every loop run so independently of the rest. Concurrently. I guess we 're talking about asynchrony... Do you know how can I "clean" my code in relation to the number of while loops I have in a single frame?

 

TY

 

0 Kudos
Message 3 of 23
(11,103 Views)

I´m Lolo... don´t know why I can´t log in with my account. Sorry about the posible confusion

0 Kudos
Message 4 of 23
(11,094 Views)

@Electro0000 wrote:

I guess we 're talking about asynchrony... 

  


Electro0000,

 

     Are you also lolobosco?

 

     If all of the While loops are basically executing the same code in parallel, then you certainly are talking about asynchrony.  We did a project a few years ago where we were monitoring behavior by taking videos, and we needed to handle 24 stations.  We wrote a single sub-VI that managed one station, then used Start Asychronous Call, with the Station Number as a parameter, and called 24 "clones" that all ran simultaneously.  Worked like a charm.

 

     Here's what you need to do to make this work:

 

  • Design a sub-VI that will handle one "instance" ("Process").  Have a parameter ("Proceso #") that will index which process you are calling.  You can use this however you need to in distinguishing your processes.
  • Configure this VI to run as a Pre-Allocated Clone.Reentrant Execution.
  • Do not use a Local Variable to stop the loops.  Use a Notifier, a VIG, or something similar that you can trace and that won't cause any race condition.
  • In a For Loop, "spawn" all of your Processes.  Do you know how to configure Start Asynchronous Call functions?  I like to use a Static VI Reference to "point to" the VI I'm spawning, right-click and make it a Strictly-typed Reference, drop a Property Node and wire the reference to get the VI Path, wire the Path and the Strictly-typed Reference to an Open Vi Reference.  Note that you need to set the Options flag for Call and Forget (x80) or Call and Collect (x100).  You might also need to include the Simultaneous Rentrant Calls (x40) flag.
  • Wire the VI Reference to the appropriate Start Asynchronous function that you intend to use.

You may notice that I'm a little vague on whether or not you need the x40 option flag.  In our code, we did not set it, yet our cameras all run simultaneously, in parallel, and their sub-VIs are definitely set to Pre-allocated Reentrant Clones.  We stop them using a VIG.

 

Bob Schor 

Message 5 of 23
(11,064 Views)

Bob,

 

Yes, I´m Lolo... 

 

It´s very interesting what you mention. I´ve never used any asynchronous app. I need to read about it and follow some rules... I´ll follow your  advice of the design.

 

Thank you very much Bob.

 

0 Kudos
Message 6 of 23
(11,050 Views)

Bob,

 

I´m not really sure how the asynchronous VI works... Would you please attach an example with 2 or 3 while loops (subVIs) running asynchronously and concurrently controlled by a main VI? I would be grateful if you could do so...

0 Kudos
Message 7 of 23
(11,030 Views)

I really liked this blog, and it might give you idea how to deal with async subVIs...

http://www.walkingthewires.com/2015/07/20/something-happened-events-in-labview/

0 Kudos
Message 8 of 23
(11,008 Views)

@lolobosco wrote:

 

I´m not really sure how the asynchronous VI works... 


Here's a very quick draft. See if it gives you any ideas.

I am using a single DVR to share information across all VIs (main and sub).

The IPE ensures proper locking.

 

(To add more processes, simply change the array size of the DVR and resize the indicator accordingly.)

 

 

 

Message 9 of 23
(10,997 Views)

Hi Blokk,

 

I like that blog as well but I guess that I need something easier...something like this: 

 

http://zone.ni.com/reference/en-XX/help/371361H-01/lvhowto/acbr_call_and_collect/

 

The problem is that my subVI is an infinite while loop and when a make an asynchronous call to it, I can´t collect its results.

 

I also have tried to make a dynamic process VI but I´ve had no luck...

0 Kudos
Message 10 of 23
(10,994 Views)