Questions tagged [usleep]

usleep is a function provided by various languages and run times that delays a script or program by an amount of time specified in micro-seconds (µ - micro).

Details of how to use usleep vary by implementation.

94 questions
4
votes
2 answers

Replacing usleep with nanosleep

I want to replace obsolete usleep function with nanosleep in my code: static int timediff( struct timeval *large, struct timeval *small ) { return ( ( ( large->tv_sec * 1000 * 1000 ) + large->tv_usec ) - ( ( small->tv_sec…
user1840007
  • 615
  • 1
  • 10
  • 19
3
votes
1 answer

usleep function is slow on Windows 10 compared to Windows 7

I made some programs in C using Dev C++ that use the usleep function, which prints characters individually slower or faster, set by a number I want: void lyrics(char *s, unsigned ms_delay){ unsigned usecs = ms_delay * 1000; /* 1000 microseconds…
SBlaster98
  • 31
  • 7
3
votes
1 answer

More precise sleep (usleep) in Python?

I'm trying to implement real-time plotting in Python, with samples being around 500-1000 microseconds apart. Using time.sleep() between drawing each sample doesn't work due to reasons mentioned here: accuracy of sleep(). I'm currently doing busy…
A6SE
  • 177
  • 1
  • 11
3
votes
1 answer

usleep inside loop takes too long

In the below C++ program, I am using the function usleep() to sleep for a 1.5 seconds. I implemented that in 2 equivalent methods as illustrated below: #include #include using namespace std; int main() { //METHOD #1 …
Ahmed Hussein
  • 715
  • 1
  • 15
  • 38
3
votes
2 answers

Bad precision of usleep when is executed in background thread in Swift

I have been using "usleep" to stop a thread during some milliseconds and I have checked that is stopping more time than expected. I am sure I am doing something wrong, I am not an expert in Swift, but I don't understand it because it is very easy to…
J.S.R - Silicornio
  • 1,111
  • 13
  • 16
3
votes
1 answer

usleep vs std::this_thread::sleep_for, when write/read on a linux serial port

I have created a c++ app that needs to connect to a modem via a serial port in order to give AT commands. I've followed the following answer: how to open, read, and write from serial port in C and it works great. In some point of the code it is…
dk13
  • 1,461
  • 4
  • 20
  • 47
3
votes
1 answer

Do sleep and usleep behave differently internally, in PHP?

I have a socket server listening to a connection for incoming data and writing outgoing data during pauses in input. Since this is in a loop I am making a sleep call in order to allow other processes to have some CPU time while my process is not…
3
votes
0 answers

golang profile runtime.usleep at 70%

I am writing a small tool in golang that reads in CSV file and passes each row to a channel. There is a receiver go-routine for every row. Somehow that is very slow - using profiling I found that the program is 68% on runtime.usleep. I have no…
Michael Miller
  • 701
  • 4
  • 6
3
votes
0 answers

Why does POSIX restrict usleep argument to less than one million

POSIX standard for usleep clearly states: The useconds argument shall be less than one million. Why is it so? Are there any known operating systems/libraries that actually enforce this restriction? Man pages for Linux, macOS, FreeBSD and Solaris…
3
votes
2 answers

Alternate ways to delay my program without sleeping because of the use of asynchronous IO in C++?

I have a questions regarding alternate ways to delay a program in C++ besides sleeping. I have a part of my program that receives and sends UDP packets to a controller. This part of the program has to run asynchronously. In order to reduce the…
3
votes
1 answer

Linux thread sleep vs read

In my application there is a Linux thread that needs to be active every 10 ms, thus I use usleep (10*1000). Result: thread never wakes up after 10 ms but always after 20 ms. OK, it is related to scheduler timeslice, CONFIG_HZ etc. I was trying to…
Rafi
  • 161
  • 5
3
votes
1 answer

Clock() return value 0 in usleep loop with more orders

I'm trying to see how much time cost execute some code in a thread. But clock() is returning 0. This is the code: int main(int argc, char *argv[]) { int begin, end; float time_spent; clock_t i,j; struct timeval tv1; struct timeval tv2; for(i = 0;…
vgonisanz
  • 11,831
  • 13
  • 78
  • 130
2
votes
1 answer

gnuplot and usleep on C

i'm trying to do a realtime plot on Linux. Everything runs ok, it's just that when the program ends, there's a lot of CPU usage for a while. Besides, because i use a very short time on usleep (like 0.3 sec) i don't know why the bash shell is…
PerroNoob
  • 843
  • 2
  • 16
  • 36
2
votes
2 answers

How inaccurate cpp's usleep function can be?

I want to measure how inaccurate usleep can be. I mean , if i write in my program usleep(5000) what is the maximum time the sleep will be? thanks in advance
kakush
  • 3,334
  • 14
  • 47
  • 68
2
votes
2 answers

Request to Apache server in chat app

I'm writing a chat application for joomla (apache server) and use this construction to emulate long-polling (server side): function get_messages($last_id) { $time = time(); while((time() - $time) < 25) { $sql = 'SELECT * FROM…
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60