From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

Wiring a value of 0 to the milliseconds to wait

Hi...

In the wait millisecond timer help it says "Wiring a value of 0 to the milliseconds to wait input forces the current thread to yield control of the CPU"

I couldn get the exact meaning of the sentence. Does this mean that the LabVIEW thread gives its control to the CPU...?

What exactly happends if we Wire a value of 0 to the milliseconds to wait. what is its Advantage...?

0 Kudos
Message 1 of 10
(7,242 Views)

Dear lordsathish,

 

Let's say you have a while loop without a "wait" function, it will eat 100% processor resources and any thread with the same  priority won't be able to access the processor.

Now if you get the same while loop and put a wait 0 ms inside, the behavior will be much different. Everytime the wait is executed the thread that keep the while loop alive will go in the last position of the thread-queque the processor is executing. It means that now processor resources are shared among threads. Basically you're asking your while loop to leave a little time to the other threads with the same priority without waiting an exact amount of ms.

 

Best regards

 

FiloP
It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong.
Richard P. Feynman
Message 2 of 10
(7,228 Views)

Hi jon snow,

Even i tried executing a while with a stop button. with a 0 wired to a millisecon wait timer.

I still found the CPU usage to raise to 100%.

Does this means that the thread in which while loop was executed now allows all other threads to be executed in the mean time again and again occupies the CPU...?

0 Kudos
Message 3 of 10
(7,210 Views)

No.

the CPU usage will remain 100% anyway.

Let's see this way the CPU is like a post office operator, it serves request from a queque of people. ( threads ).

When you don't use wait your thread will use the operator until it's finished.

When you set wait =0ms every time he has to wait it simply free the operator for the next guy in the queque and goes to the end of the queque.

When you set wait = 10ms every time he has to wait it goes sleeping for 10ms and then goes to the end of the queque waiting it's turn. 

 

You may see the second case doesn't leave the post office operator free thus it will work at 100% pace anyway.

 

I hope this will clarify a little.

Bye

 

FiloP
It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong.
Richard P. Feynman
Message 4 of 10
(7,206 Views)

sorry the answer to your direct question

 

Does this means that the thread in which while loop was executed now allows all other threads to be executed in the mean time again and again occupies the CPU...?

 

 

 

is YES

Bye

 

FiloP
It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong.
Richard P. Feynman
Message 5 of 10
(7,203 Views)

Nice explanation Jon. Well done.

 

I occasionally (often?) wire a zero (0) to a Wait ms inside a loop.  Especially if I want the process to occur as fast as possible.  You simply need to be careful when implementing it, which depends on what else is running in parallel and how you want the timing among tasks to occur.

 

The short answer is that it is perfectly okay to wire a 0. 🙂

 

R

Message 6 of 10
(7,175 Views)

A simple test case would be to place two such loops on the diagram so both run at the same time. We have two scenarios:

  1. The two loops don't contain any wait statement
  2. each loop contains a 0ms wait

If there are no waits, loop 1 will spin for a while then loop 2 will spin for a while. Each loop will hold on for 55ms. 100% of the CPU is used. For small loops of finite duration (e.g. a newton-raphson iterative algorithm), this is desired. You want to get one loop done and not constantly switch to other structures.

 

If each loop contains a wait, loop 1 will spin once and relinquishes control and now loop 2 will spin once. Each wait will allow other structures that are ready to execute to actually execute. You still use 100% of the CPU. Overall, the program has probably more overhead because of the constant switching but all parts of the code get a fair chance to actually run.

 

If you now add a 1ms wait instead of 0ms, the CPU useage drop to negligible levels.

 

Now imagine you have 100 such loops on the diagram. If none have waits, each loop will grab the CPU for 55 milliseconds and any particular loop might have a very long time (>5seconds!)  before it can execute again. The user interaction with such a program might be very usatisfactory.

 

All loops that run continuously, especially polling loops, need a reasonable wait.

 

 

Message 7 of 10
(7,157 Views)
OF course if you have multiple CPU cores, the behavior in the above scenario will be more complicated.
Message 8 of 10
(7,147 Views)

Here is a quick demonstration (LabVIEW 8.0) where you can run two parallel loops, either with or without a 0ms wait.

 

If you set N to a much higher number, you'll see that the loops alternate at some somewhat unpredictable (but slow) rate if there is no wait.

 

(Important: This is actually very problematic code and its only purpose is to demonstrate the topic of this thread. It is required to set the VI to run in the UI thread, else things go all over the place due to race conditions and the output array may contain less than 50% of the expected elements in some trials).

 

 

Message 9 of 10
(7,118 Views)

Hi altenbach,

The VI you provided explained it pretty well,Smiley Happy

Thanks...

0 Kudos
Message 10 of 10
(7,059 Views)