How to Cause a LabWindows™/CVI™ Process or Thread to Pause Execution?

Updated May 11, 2018

Reported In

Software

  • LabWindows/CVI Base
  • LabWindows/CVI Full

Issue Details

Using the LabWindows™/CVI Delay function causes CPU utilization to go to 100%. Is there a better way to cause a LabWindows/CVI process/thread to pause execution?

Solution

In a LabWindows/CVI application in which you want to pause execution of the current thread, but do not want system performance degraded by 100% CPU utilization, the Windows SDK Sleep() function is the best choice.

 

Additional Information

When you call the LabWindows/CVI Delay(double Number_of_Seconds) function, LabWindows/CVI waits for the specified number of seconds before executing the next line of code. However, this is a "busy wait" loop in which the CPU is utilized 100%.

On the other hand, the Windows SDK includes the Sleep(long dwMilliseconds) function, which suspends the execution of the current thread for a specified number of milliseconds. This yields the CPU to other threads and processes, so the current LabWindows/CVI application thread does not tie up the computer's CPU.

You can demonstrate this behavior by running the following sample code as a LabWindows/CVI console application. On the test machine (Pentium III 1GHz, 256MB RAM), the CPU utilization was at 100% during the Delay(0.01 sec) function and at about 10% during the Sleep(10 ms) function:

#include <windows.h>
#include <ansi_c.h>
#include <utility.h>

int main (int argc, char *argv[])
{
   int i=0;

   printf("delaying 10ms (watch CPU)...\n");
   for (i=0; i<1000; i++) {
      Delay(0.01);
   }

   printf("sleeping 10ms (watch CPU)...\n");
   for (i=0; i<1000; i++) {
      Sleep(10);
   }

   return 0;
}

WAS THIS ARTICLE HELPFUL?

Not Helpful