hi

i am doing a project and i would like to know if anyone can please tell me how to write a delay or wait function in c++ which works asynchronously with other programs. :confused: Thank you for ur help.

gmv

Dani AI

Generated

The thread covers the basic idea but misses a modern, portable pattern and a few practical tips. asked for an asynchronous delay; and pointed out that the simple OS sleep call blocks the calling thread and that short waits (20 ms) can be hard to observe. also noted platform differences. Below are concise, practical options and cautions that remain valid years later.

A simple, portable way to perform a delay without blocking the main flow is to run the wait inside a detached worker thread and invoke a callback when the delay expires:

#include <thread>
#include <chrono>
#include <functional>

void delayed_call(std::chrono::milliseconds delay, std::function<void()> job) {
    std::thread([delay, job=std::move(job)]() {
        std::this_thread::sleep_for(delay);
        job();
    }).detach();
}

For waits that must be interruptible (able to wake early), use a condition variable instead of an uninterruptible sleep. This lets another thread cancel the wait cleanly:

#include <condition_variable>
#include <mutex>

std::mutex m;
std::condition_variable cv;
bool cancel = false;

bool wait_or_cancel(std::chrono::milliseconds timeout) {
    std::unique_lock<std::mutex> lk(m);
    return !cv.wait_for(lk, timeout, []{ return cancel; }); // true on timeout
}

Notes and troubleshooting: prefer std::chrono::steady_clock for deadline math to avoid system-clock jumps; use sleep_until for periodic tasks to reduce drift; avoid busy-wait loops (high CPU). OS scheduling means any sleep can overshoot; for sub-millisecond precision use platform-specific high-resolution timers and accept tradeoffs in power and complexity. Finally, never block a GUI thread—run delays on background threads or use a timer facility tied to the GUI framework.

Recommended Answers

All 7 Replies

hi

i am doing a project and i would like to know if anyone can please tell me how to write a delay or wait function in c++ which works asynchronously with other programs. :confused: Thank you for ur help.

gmv

Use Sleep( int waitTime ) in windows. It has only a resolution of 1 millisecond however. Dont know about cases when you want to wait for shorter times.

Use Sleep( int waitTime ) in windows. It has only a resolution of 1 millisecond however. Dont know about cases when you want to wait for shorter times.

Hi,
Thank you very much for your reply.
i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc asynchronous?

gmv

Hi,
Thank you very much for your reply.
i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc asynchronous?

gmv

It should work. The function will wait for at least 20 milliseconds. Was that the time you wanted the function to wait? THe Sleep function is independent of the other Processes or threads. It makes the calling thread wait for at least the time specified.

Since it's in milliseconds, you might want to do something like 5000 to notice it. In unix, it's lowercase sleep, and it's in seconds..

Hi,
Thank you very much for your reply.
i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc c?

gmv

What makes you think it doesn't work? 20 milliseconds is not a very long time :eek: During the sleep period you program get no cpu time because the os's thread scheduler does not give it any. However, MS-Windows os is not rocket science, so don't expect the sleep period to be very exact -- meaning that the program may sleep anywhere from 1 to 200 or so milliseconds.

Ya you might want to include <time.h> , it works for me anyway .

Let sleeping threads lie.

If you must open a new thread, [thread=35517]refer back to the original[/thread].

But then you might realize that a new thread would be relatively pointless, much like replying to a dead thread.

commented: Stop threadlocking! I needed this post! +0
commented: Ignoring the 0-posting whiner de-rep +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.