Solved

Sleep 100 or 10 microseconds in visual c++

Posted on 2004-09-03
8
6,745 Views
Last Modified: 2013-11-20
Hi there,

    I am looking for a routine to have my program sleep for 10 or 100 microsecnds without using full processor use.   I saw some good topics already posted but they use full processor time.  The routine could be timer or a sleep.  It does not have to be to the microsecond accurate but very close.

Thanks,

Bill
0
Comment
Question by:WilliamSolberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11972109
You could use this in a loop (with a PeekMessage to remove and process any pending messages)

INFO: Timer Resolution in Windows NT and Windows 2000

Q115232


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Win32 Application Programming Interface (API), used with:
the operating system: Microsoft Windows NT, versions 3.1, 3.5, 4.0
the operating system: Microsoft Windows 2000

--------------------------------------------------------------------------------


SUMMARY
In Win32-based applications, the GetTickCount() timer resolution on Windows NT, and Windows 2000 can be gotten by calling GetSystemTimeAdjustment() and checking the value returned in the lpTimeIncrement parameter. This value is indicated in 100 nanosecond units and should be divided by 10,000 to achieve a value in terms of milliseconds.

NOTE: The measurements in milliseconds indicate the period of the interrupt, not the units of the returned value.

The GetTickCount() API should not be used for resolution critical algorythms. Instead, QueryPerformanceCounter() and QueryPerformanceFrequency() should be used.

The Win32 API QueryPerformanceCounter() returns the resolution of a high- resolution performance counter if the hardware supports one. For x86, the resolution is about 0.8 microseconds (0.0008 ms). You need to call QueryPerformanceFrequency() to get the frequency of the high-resolution performance counter.
0
 
LVL 13

Expert Comment

by:SteH
ID: 11972131
The problem you are facing here is the OS latency. A "sleep" that not uses full processor time gives CPU slices to other processes including the OS itself. Latter has normally a reaction time of several ms (10s of ms) so a sleep of 10 microseconds not having control over the processor will only be accurate to about the OS latency of ms. So I doubt it usefullness sleeping for attempted 10 us with an error of +- 10 ms.

When you don't give away the processor control you can do better but even when not using it. But the taskmanager will report 100% load due to other processes not being able to get CPU slices.
0
 

Author Comment

by:WilliamSolberg
ID: 11976205
An example on use of PeekMessage In a loop for this purpose would be greatly appreciated.  I was unable to find that Article Q115232.

Thanks,

Bill
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 11980848
   while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
       TranslateMessage(&msg);
       DispatchMessage(&msg);  
    }


Q115232 - have you tried micosoft.com ? I got this from my MSDN copy.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11981373
> I am looking for a routine to have my program sleep for 10 or 100 microsecnds without using full processor use.

The WinAPI's Sleep() function doesn't use full processor time. So what is the purpose of your question? Do you want to make your own function, or maybe do you want to make sleep a thread while the other continues working?
0
 

Author Comment

by:WilliamSolberg
ID: 11985111
jaime,

the WinAPI's Sleep function will wait for 10 milliseconds and can be lowered to 1 Millisecond, but that is too long for my application..

The purpose of the short wait is that I want to read the state of a parellel port about every 10us (microseconds) but it cannot exceed 100us for acurrecy.

a microsecond is 1/1000 of a millisecond
0
 
LVL 13

Expert Comment

by:SteH
ID: 11987648
WilliamSolberg,

but in that case my comment applies. Yo won't be able to assure this short timing due to OS latencies. For NT type systems this is ~ 10 ms for '9x this  is around 40 ms. So either you control the CPU not giving control to any thread (especially the OS like AndyAinscow's comment does when processing messages) and hope that the system is not demanding it: when your timeslice has expired another thread including the OS can get the next one and you could not get time for several milliseconds.

If you need to react in the microsecond range a RealTime (RT) OS is mandatory!

Or do the processing in a embedded device which has a big buffer on the side towards the PC.

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11987773
If it has to be max 100 microsecond intervals you will have to control the processor.  My suggestion is a way to do constant checks without controlling the procesor but then one is at the mercy of the OP system assigning time slices to your app.

Is it logging or controlling?
Logging - consider something like a device driver that runs at kernal level and puts the values of the port into a file/pipe for your app.
Controlling - run-time level priority OR dig out those old DOS disks and do a dos based app.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question