English 中文(简体)
是否有一对等的电离窗?
原标题:Is there a Windows equivalent of nanosleep?
  • 时间:2011-10-19 19:35:33
  •  标签:
  • winapi

Unix has a variety of sleep APIs (sleep, usleep, nanosleep). The only Win32 function I know of for sleep is Sleep(), which is in units of milliseconds.

我看到,大多数睡觉,即使是在Unix的睡觉也大为四舍五入(即:通常约10米)。 我已看到,在太阳岛,如果你是生根的话,你就可以睡觉,我知道,如果能够用精细的粮时计参数,也有可能在HPUX上这样做。 是否可在视窗上提供更精细的微粒度时器,如果是的话,那么对应器是什么?

最佳回答

可悲的事实是,对此没有很好的答案。 多媒体时间可能是你最接近的——他们只允许你将时间定在1米以下:but<>>>。 (timeBeginPeriod) 它们实际上提供了大约1个ms的精度,其中多数人通常只有大约10-15个ms。

还有许多其他候选人。 首先,CreateWaitabletimerSetWaitabletimer似乎最接近,因为它们在100个实习生中重新设置。 不幸的是,你确实可以依赖接近于这一解决办法的任何地方,至少在我的测试中。 从长远来看,它们可能提供最佳的可能性,因为它们至少让你确定一个少于1点的时期,尽管你目前可能取决于决议的执行(任何地点接近)。

<代码>NtDelayExecution与<代码>SetWaitabletimer大致相同。 除无证件者外。 除非你重新确定使用/测试无证职能,否则在我看来,<代码>。 创建Waitabletimer/-SetWaitabletimer是在有文件记录的基础上作出更好的选择。

如果你重新使用校对池,你可以使用<代码>CreateThreadPooltimer<>和<编码>。 我已经对它们进行了充分的测试,以便对其真正提供的决议有确定性,但我并不感到特别乐观。

时间查询(Createtimer Queue,Createtimer Queuetimer等)是作为更换多媒体时间的建议,但(至少在我的测试中)它们实际上并未提供比更好的解决办法。 睡觉/代码。

问题回答

如果你只希望 Resolution in the nanoseconds range,那么NtDelayExecution ,载于ntdll/code>:dll <>:>:

NTSYSAPI NTSTATUS NTAPI NtDelayExecution(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);

它每隔100天采取措施。

HOWEVER, this probably isn t what you want:

It can delay for much longer than that—as long as a thread time slice (0.5 - 15ms) or two.
Here s code you can use to observe this:

#ifdef __cplusplus
extern "C" {
#endif
#ifdef  _M_X64
typedef long long intptr_t;
#else
typedef int intptr_t;
#endif
int __cdecl printf(char const *, ...);
int __cdecl _unloaddll(intptr_t);
intptr_t __cdecl _loaddll(char *);
int (__cdecl * __cdecl _getdllprocaddr(intptr_t, char *, intptr_t))(void);
typedef union _LARGE_INTEGER *PLARGE_INTEGER;
typedef long NTSTATUS;
typedef NTSTATUS __stdcall NtDelayExecution_t(unsigned char Alertable, PLARGE_INTEGER Interval); NtDelayExecution_t *NtDelayExecution = 0;
typedef NTSTATUS __stdcall NtQueryPerformanceCounter_t(PLARGE_INTEGER PerformanceCounter, PLARGE_INTEGER PerformanceFrequency); NtQueryPerformanceCounter_t *NtQueryPerformanceCounter = 0;
#ifdef __cplusplus
}
#endif

int main(int argc, char *argv[]) {
    long long delay = 1 * -(1000 / 100) /* relative 100-ns intervals */, counts_per_sec = 0;
    long long counters[2];
    intptr_t ntdll = _loaddll("ntdll.dll");
    NtDelayExecution = (NtDelayExecution_t *)_getdllprocaddr(ntdll, "NtDelayExecution", -1);
    NtQueryPerformanceCounter = (NtQueryPerformanceCounter_t *)_getdllprocaddr(ntdll, "NtQueryPerformanceCounter", -1);
    for (int i = 0; i < 10; i++) {
        NtQueryPerformanceCounter((PLARGE_INTEGER)&counters[0], (PLARGE_INTEGER)&counts_per_sec);
        NtDelayExecution(0, (PLARGE_INTEGER)&delay);
        NtQueryPerformanceCounter((PLARGE_INTEGER)&counters[1], (PLARGE_INTEGER)&counts_per_sec);
        printf("Slept for %lld microseconds
", (counters[1] - counters[0]) * 1000000 / counts_per_sec);
    }
    return 0;
}

我的产出:

Slept for 9455 microseconds
Slept for 15538 microseconds
Slept for 15401 microseconds
Slept for 15708 microseconds
Slept for 15510 microseconds
Slept for 15520 microseconds
Slept for 1248 microseconds
Slept for 996 microseconds
Slept for 984 microseconds
Slept for 1010 microseconds

The MinGW answer in long form:

MinGW and Cygwin provides a nanosleep() implementation under <pthread.h>. Source code:

  • In Cygwin and MSYS2: signal.cc and cygwait.cc (LGPLv3+; with linking exception)
    • This is based on NtCreateTimer and WaitForMultipleObjects.
  • In MinGW-W64: nanosleep.c and thread.c (Zope Public License)
    • This is based on WaitForSingleObject and Sleep.

In addition, gnulib (GPLv3+) has a higher-precision implementation in nanosleep.c. This performs a busy-loop over QueryPerformanceCounter for short (<1s) intervals and Sleep for longer intervals.

您可使用通常的<代码>全程,即与所有基本异构体相联的切除器。

视窗提供Multimedia timers,其分辨率高于Sleep()。 该办公室支持的实际决议可在操作时间获得。

你不妨研究

timeBeginPeriod / timeEndPeriod

and/or

QueryPerformanceCounter

See here for more information: http://www.geisswerks.com/ryan/FAQS/timing.html particulary the section towards the bottom: High-precision Sleeps

Yeah有编织物。





相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...