hey guys,

I need this

Var1 = 0;
Sleep(500);
Var1 = 1;

My problem is that the Sleep makes all my threads sleep.

I want only the one thread I'm in sleep.

Thanks,

Jeff

Dani AI

Generated

As and hinted, the call that suspends execution affects the thread that invoked it. If other threads appear to stop when one thread sleeps, the cause is usually higher-level: a held lock, a blocked message loop, or waiting threads that depend on the sleeping thread for a signal.

Common root causes and fixes

  • Holding a mutex/critical section while sleeping. If Thread A takes a lock, sleeps, then releases it, Thread B will block while A sleeps. Fix: avoid sleeping while holding locks; use condition variables or release the lock before waiting.
  • Sleeping the UI/main thread. Timers and message processing run on the main loop; putting that thread to sleep makes the app look frozen even though worker threads may still run. Move long work to a background thread or use asynchronous timers.
  • Using Sleep as synchronization (polling). Replace polling with proper primitives: std::condition_variable, OS events, or futures, and use timed waits instead of blind sleeps.

Small practical patterns

  • Prefer C++11 threading helpers like std::this_thread::sleep_for for portable per-thread delays (std::this_thread::sleep_for).
  • Replace polling with a condition_variable and wait_for/notify_one so a thread sleeps only until a meaningful event occurs.

Debug checklist

  • Break into the debugger and inspect each thread's call stack and held synchronization objects.
  • Search code for long-held locks, Sleep calls within critical sections, or main-thread sleeps.
  • Create a minimal repro: isolate the sleep and see whether other threads still progress.

Windows reference for the API: Sleep (Win32).

Recommended Answers

All 2 Replies

Sleep() only works for the current thread, it does not affect other threads. So if you think other threads are also sleeping then you are doing something wrong or you do not understand the code you wrote.

If you put that same Sleep() at the same place in the code for each thread then each of them may sleep at the same time and confuse you, the programmer. If that is the case, then maybe call Sleep() with a different parameter value for each thread so that they are not all simultaneously sleeping for the same amount of time.

Based on the code posted, I assume that it is running on the main thread. Calling Sleep() from the main thread causes the program to pause for the specific amount of time. All others background threads will still be running and doing some work.
If you need some background work to be done while the current(main) thread is sleeping, try to spawn a background thread. More code is needed to analyze what is happening in your case.

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.