Skip to content

Acurrate/Precise/Stable/Cross-platform sleep() #786

@alichraghi

Description

@alichraghi

Windows

This blog post nicely explains everything we need to know about sleeping threads in windows. TLDR; i ran the benchmarks locally and all those solutions are extremely precise and has similar CPU usage except for the the new waitable timer flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION added in Windows 10 which has an stable CPU usage even in 10ms scheduler. i believe we should listen to author's advice and go with the win32 Waitable Timers API

Linux/Android

Easy choice. We use the high-resulotion sleep function (clock_nanosleep) with CLOCK_MONOTONIC flag. there's also the POSIX function nanosleep(), but it's just clock_nanosleep() with extra overhead.

WASM

std::chrono::high_resolution_clock + std::this_thread::sleep_for

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions