From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

simple multi thread example

Hello I am still confused as to how to run parallel processes. I am looking for something very simple so that I can understand better. If possible I would like to see an example that basically starts a vi that writes to a file. For example the vi has a for loop that runs 10 times each time it generates a new instance of a program that writes the number associated with the for loop to the file 2 times. So basically after the program is run the file will read.1122334455667788991010...or have some numbers mixed up depending on how fast each thread ran. I guess the vi would have some controls on the front to adjust the number of times so that the user could get the idea of how the threads would end up writing of order...This is a common example similar to "hello world" of multi threading programs in most languages so I figured that there would be something like this somewhere...but I can't find it. If anyone can point me in the right direction I would appreciate it thanks.
0 Kudos
Message 1 of 8
(9,282 Views)

I'm not clear on why you want to "generate a new instance of a program that writes the number associated with the FOR loop to the file 2 times."

 

If that's the result you want, there are certainly easier ways.

 

If you're just experimenting with multi-threading, then you can spawn instances of a VI, without spawning instances of a PROGRAM.  You have to make them REENTRANT  to have them truly independent.

 

If you're just experimenting with parallelism, be aware that you can run any number of FOR loops in parallel, and they will indeed run in parallel. No special action required.

 

Perhaps if you explained what you are looking for a bit deeper. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 2 of 8
(9,274 Views)
I am just experimenting at this point. I do like the how easy it is to run parallel tasks...I have attached a very simple example, but I need to be able to create a new process depending on the state of input data and there may be hundreds of processes that are required to be started so I cannot just hard code the processes they need to be spawned so I was just wondering if there were some really simple examples of how to generate parallel process as the program is running.
0 Kudos
Message 3 of 8
(9,257 Views)

well the stop button does not work but you get the picture. I basically need to start with one of the while loops and based on an input I need to generate another while loop, then on and on...but they must all be started and run in parallel. any help is appreciated thanks.

 

0 Kudos
Message 4 of 8
(9,256 Views)

well the stop button does not work

The STOP button doesn't work because you read it once and THEN start four loops.  So the value that it read when started is remembered, and it doesn't read the button again.

 

Change that to a SWITCH and read it INSIDE each loop separately. (Use local variables). 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 5 of 8
(9,235 Views)

Hmmm. I cannot locate a good example for you, but I know it's possible.

 

So I created my own example  (I THINK I saved it in 8.5 format)

 

 

YOU NEED TO HANDLE ERRORS - this example doesn't.

 

But it does do what you asked.

 

It spawns any number of subVIs, all of which run independently. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 6 of 8
(9,224 Views)

I should point out that there is a race problem with that example.

Because of the way that it READs the current text, MODIFYs it by adding to it, and WRITEs it back, there is a chance that two threads could read the text, both modify it (in different ways), and  both write it back, but only one of the modifications would be remembered.

 

The real solution should use a non-reentrant VI  to handle that chore.  That way it could only be called from one VI at a time. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 7 of 8
(9,196 Views)

This Nugget on Occuences and running VI's in parallel may be helpful.

 

It explores the question how can we build a real Animusiac app

 

 

 

The example i posted there launches and runs a bunch of background threads.

 

 

 

There was a good follow-up discusion in that thread about multiple threads etc.

 

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 8 of 8
(9,176 Views)