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
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
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
std::condition_variable, OS events, or futures, and use timed waits instead of blind sleeps.Small practical patterns
std::this_thread::sleep_for for portable per-thread delays (std::this_thread::sleep_for).condition_variable and wait_for/notify_one so a thread sleeps only until a meaningful event occurs.Debug checklist
Sleep calls within critical sections, or main-thread sleeps.Windows reference for the API: Sleep (Win32).
Jump to Post— Ancient Dragon 5,243Sleep() 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 …
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.