Cross Platform Sleep Function for C++

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.

What should I use to stop the sleep() function in C if a condition changes in that period of time (doesn't hold anymore)?

I would not call either MS Sleep() or gcc sleep() but go for a non-blocking function, as you have with MS kbhit().

For a 1 second timer an example use is

clock_t target = clock() + CLOCKS_PER_SEC;
while(!kbhit() && clock() < target) {
// some code if needed
}

The loop will end when a key is pressed, or after 1 second. You can check kbhit() again to find out which.

I tried to come up with a cross platform alternative to sleep(), but my code isn't quite working

time() and difftime() have a resolution of a second, to there's no
way to use them to manage intervals of less than a second; even for
intervals of a second, they're not usable, since the jitter may be up to
a second as well.

In this case, the solution is to define some sort of timer class, with a
system independent interface in the header file, but system dependent
source files; depending on the system, you compile one source file or
the other. Both Windows and Linux do have ways of managing time with
higher resolution.

What is the proper #include for the function 'sleep()'?

The sleep man page says it is declared in <unistd.h>.

Synopsis:

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

Is there a wait function in C++?

Since C++11, you might use std::this_thread::sleep_for

using namespace std::chrono_literals;

std::this_thread::sleep_for(250ms);

Is there an alternative sleep function in C to milliseconds?

Yes - older POSIX standards defined usleep(), so this is available on Linux:

int usleep(useconds_t usec);

DESCRIPTION

The usleep() function suspends execution of the calling thread for
(at least) usec microseconds. The sleep may be lengthened slightly by
any system activity or by the time spent processing the call or by the
granularity of system timers.

usleep() takes microseconds, so you will have to multiply the input by 1000 in order to sleep in milliseconds.


usleep() has since been deprecated and subsequently removed from POSIX; for new code, nanosleep() is preferred:

#include <time.h>

int nanosleep(const struct timespec *req, struct timespec *rem);

DESCRIPTION

nanosleep() suspends the execution of the calling thread until either at least the time specified in *req has elapsed, or the
delivery of a signal that triggers the invocation of a handler in the
calling thread or that terminates the process.

The structure timespec is used to specify intervals of time with nanosecond precision. It is defined as follows:

struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

An example msleep() function implemented using nanosleep(), continuing the sleep if it is interrupted by a signal:

#include <time.h>
#include <errno.h>

/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(long msec)
{
struct timespec ts;
int res;

if (msec < 0)
{
errno = EINVAL;
return -1;
}

ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;

do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);

return res;
}

Precise thread sleep needed. Max 1ms error

From the question tags I suppose you are on windows.
Take a look at Multimedia Timers, they advertise precision under 1ms.
Another options is to use Spin Locks but this will basically keep a cpu core at maximum usage.

Run a function x amount of times then sleep for a period before going again

You can use a for loop so that you can keep track of entries that has been processed.

library(dplyr)
library(tidyr)

result <- vector('list', nrow(examples))

for(i in seq(nrow(examples))) {
#Sleep for 1 minute after every 2 values
if(i %% 2 == 0) Sys.sleep(60)
result[[i]] <- npi::npi_search(examples$names[i])
}

bind_rows(result) %>%
select(addresses) %>%
unnest(addresses)

# country_code country_name address_purpose address_type address_1
# <chr> <chr> <chr> <chr> <chr>
# 1 US United Stat… LOCATION DOM 6601 PHO…
# 2 US United Stat… MAILING DOM 800 MARS…
# 3 US United Stat… LOCATION DOM 5835 NE …
# 4 US United Stat… MAILING DOM 2100 DOU…
# 5 US United Stat… LOCATION DOM 1163 BLA…
# 6 US United Stat… MAILING DOM 1163 BLA…
# 7 US United Stat… LOCATION DOM 500 COMM…
# 8 US United Stat… MAILING DOM 150 SCHA…
# 9 US United Stat… LOCATION DOM 6420 CLA…
#10 US United Stat… MAILING DOM 6420 CLA…
#11 US United Stat… LOCATION DOM 118 W NO…
#12 US United Stat… MAILING DOM PO BOX M
#13 US United Stat… LOCATION DOM 520 S SA…
#14 US United Stat… MAILING DOM 520 S SA…
#15 US United Stat… LOCATION DOM 910 S. H…
#16 US United Stat… MAILING DOM 24 ROY S…
#17 US United Stat… LOCATION DOM 1595 HAR…
#18 US United Stat… MAILING DOM 1595 HAR…
# … with 6 more variables: address_2 <chr>, city <chr>, state <chr>,
# postal_code <chr>, telephone_number <chr>, fax_number <chr>


Related Topics



Leave a reply



Submit