WASM Module with Sleep funtion

I’m working on a C++ wasm module, and I need to add a “sleep” at a certain point.

I can’t use the Sleep() function from Windows.h (not supported in WASM)
and I haven’t had any success with Clock() either, the sim doesn’t support it.

Does anyone have an idea or has managed to use or code a “Sleep” in WASM?

D.

That’s one thing I don’t miss from the lack of threading support… don’t sleep() in threaded programs unless you absolutely have no other choice (wait handles, mutexes, and such).

Anyway, if you really need to pause for a certain amount of time, you could use a timer:

#include <chrono>

const auto endTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(1000);
while (endTime > std::chrono::steady_clock::now()) { /* do nothing */ }

HTH,
-Max

Thanks for the answer, I tried something similar with chrono + thread and the sleep_for function, but no success either.

Your timer works but it somehow captures the processing of the rest of the sim generating a complete freeze for the timer period.

maybe because I’m executing it inside the Dispatcher CallBack cycle

I will do some tests.

D.

Well, the module is single-threaded, so that’s to be expected. You’d have the same result with sleep().

What does your standalone module do that isn’t triggered from the dispatch handler originally? For any concurrency to happen there would need to be some kind of “external” trigger like another callback happening at the same time, and AFACT there’s only the init/deinit and the SimConnect message dispatcher which call anything within the module.

Nothing from std concurrency lib is available… no thread, mutexes, atomics, wait handles, etc. And since Win32 isn’t supported either, that means no corresponding MSVC features or timers.

Cheers,
-Max

you can create your own message timer service even in a single thread…
create a timer event once a second or once every x seconds … set variable int to coundDown seconds sleeping when you want a sleep period

  1. = increment a timer counter of seconds
    2 if there are any timer sleep/wake alerts active decrement the count by 1 (such wakeTimer1Seconds, wakeTimer2Seconds ) (watcher)
  2. = send my self a message (“sleep” (do nothing) but wake in 2 seconds) (set wakeTimer1Seconds = 2
  3. if a wake counter <= 0 then end sleep state end

if you do any other actions (keyboard , mouse) during the sleep , then set sleep to 0 to turn off watcher

Are you talking about MSFS WASM specifically? There’s nothing to wake up the timer. There’s no concurrency possible.

How? In the context of a WASM module. There’s no concurrency possible. There’s no sleep, only CPU-eating spin loop.

-Max

After some tests and unsuccessful attempts, at least as far as I know there is no way to create timers inside the sim, as indicated by @Maxinal6379, no threads or anything that can handle independent cycles.

I have also solved my problem using Simconnect Callbacks, and although my module does not need to use Simconnect, I created a timer with a dummy callback and some counters.

D.