c++, usleep() is obsolete, workarounds for Windows/MingW?
usleep()
works with microseconds. In windows for getting microsecond precesion you should use QueryPerformanceCounter() winapi function. Here you can find how get that precesion using it.
SLEEP: (Sleep or usleep) is not suspending everything in my thread in Linux but it does in Windows? why?
The output is buffered. You don't see the dots, but they are issued like clockwork.
If you add
fflush(stdout);
or a newline to the output string, you should see the dots appear regularly.
Cross platform Sleep function for C++
Yes there is. What you do is wrap the different system sleeps calls in your own function as well as the include statements like below:
#ifdef LINUX
#include <unistd.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif
void mySleep(int sleepMs)
{
#ifdef LINUX
usleep(sleepMs * 1000); // usleep takes sleep time in us (1 millionth of a second)
#endif
#ifdef WINDOWS
Sleep(sleepMs);
#endif
}
Then your code calls mySleep
to sleep rather than making direct system calls.
implicit declaration of function usleep
That list is the pre-conditions for having usleep
defined. It's basically a C-like expression involving #define
variables which has to be true before including the header file.
The header file itself will only define usleep
inside what is usually a massive nest of #ifdef
statements and the developers have taken the time to tell you what you need to do so that you don't have to spend hours trying to figure it out yourself :-)
Assuming you're using a glibc
2.12 or better, it means you either have to:
- declare _BSD_SOURCE; or
- declare a complicated combination of three other things, which I won't bother to decode.
Probably the easiest fix is to simply compile with gcc -D _BSD_SOURCE
or put:
#define _BSD_SOURCE
in the code before you include the header file that gives you usleep
.
You'll probably want to define these before any includes in case there are dependencies between the various header files.
Sleep function in Windows, using C
Use:
#include <windows.h>
Sleep(sometime_in_millisecs); // Note uppercase S
And here's a small example that compiles with MinGW and does what it says on the tin:
#include <windows.h>
#include <stdio.h>
int main() {
printf( "starting to sleep...\n" );
Sleep(3000); // Sleep three seconds
printf("sleep ended\n");
}
Related Topics
How to Get Memory Usage Under Windows in C++
Why Does Windows 10 Start Extra Threads in My Program
Linker Error When Calling a C Function from C++ Code in Different VS2010 Project
Initialize Parent's Protected Members with Initialization List (C++)
Why Does Cudamalloc() Use Pointer to Pointer
Dynamic Aligned Memory Allocation in C++11
Why No Variable Size Array in Stack
How to Iterate Over a Packed Variadic Template Argument List
Where Is '%P' Useful with Printf
C++11 Inheriting Constructors and Access Modifiers
How to Declare a Global Variable in C++
How to Call C++ Functions from Within Ruby
Detecting Constexpr with Sfinae
"The System Cannot Find the File Specified" When Running C++ Program
Stdafx + Header File - Order of Inclusion in Mfc Application