POSIX C/C++ sleep() and usleep() not working? (Raspberry PI) - c++

I wrote a small programm on my RasPI and have trouble with the sleep() and usleep() functions. both of them don't work. When I use usleep() with a number below 1000000 (below 1 second) it works, whenever i try to use a number that should let the program sleep for 1 second or more, it doesn't work. I've been working on making the Digital pin HIGH for a given time.
I've tried to shrink the program to printf() and to sleep only:
#include <stdio.h>
#include <unistd.h>
int main()
{
while (true)
{
sleep(1);
printf("%.2f", 10.1);
}
}

works after flushing the output buffer
#include <stdio.h>
#include <unistd.h>
#include <chrono>
#include <thread>
int main()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("%.2f\n", 10.1);
}
}

Related

C++ CPU load too high for a very small program [duplicate]

This question already has answers here:
Does endless While loop take up CPU resources?
(3 answers)
why does an infinite loop of the unintended kind increase the CPU use?
(4 answers)
Does a while(true) Infinite Loop Peg the CPU? [closed]
(2 answers)
CPU usage increases for Empty Infinite While loop
(4 answers)
Closed last month.
I wrote some in c++ and try to execute it on raspberry pi . I noticed a CPU Load of 100 %
I then removed bit for bit from the code to see what causes the high load. Now my code looks like the code below (stripped of all functionality ) and it still has 99-100% load.
Can someone point me in the right direction ?
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <cmath>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
int main(int argc, char* argv[])
{
// Check command line arguments
if (argc < 3) {
cout << "Usage: Test can_name dbc_file" << endl;
return 1;
}
// Get can name and dbc file name from command line arguments
string canName = argv[1];
string dbcFileName = argv[2];
while (true) {
}
return 0;
}
I tried to strip my code of all funtionality to end up with a basic program that should have very little cpu load
Try adding a sleep within the loop like
while ( true ) {
usleep(100000); // sleeps for 100 ms
}

std::this_thread::sleep_for() sleep forever when changing OS date/time

I found std::this_thread::sleep_for()/sleep_until() may sleep forever when OS datetime changed during sleep.
Here is a sample code.
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <Windows.h>
int main()
{
for (int i = 0; i < 1000; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
//Sleep(500);
std::cout << std::to_string(i) + "\n";
}
}
When the program is running, I change the OS time a month ahead, then the program stop printing.
If I switch to Windows's Sleep() API, it works as expected.
It is a bug for std::this_thread::sleep_for()/sleep_until() or do I miss something?
Note: I am using Visual Studio 2017 and Windows 10 Pro 1909.

Making C++ pause

Is there a C++ equivalent to Python's time.sleep()?
Use boost::this_thread::sleep
// sleep for 5 seconds
boost::this_thread::sleep(boost::posix_time::seconds(5));
The following code will sleep for 10 milliseconds.
boost::this_thread::sleep(boost::posix_time::milliseconds(10))
Refer to boost::posix_time::time_duration for more ways to construct the duration.
I'm not aware of any portable function, but mainstream OSes have usleep for *nix and Sleep for Windows.
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch

Interrupt in C++

I am trying to understand interrupts and am looking for a simple code that uses interrupts. Could somebody please help me with it?
Here are two examples using the alarm function. alarm causes SIGALRM to happen n seconds after you call that function.
This program will run for 3 seconds, and then die with SIGALRM.
#include <signal.h>
#include <unistd.h>
int main() {
alarm(3);
while(true);
}
In this case, we'd like to catch SIGALRM, and die gracefully with a message:
#include <signal.h>
#include <unistd.h>
#include <iostream>
volatile bool alarmed = false;
void alrm_handler(int) {
alarmed = true;
}
int main() {
signal(SIGALRM, alrm_handler);
alarm(3);
while(not alarmed);
std::cout << "done" << std::endl;
}

How do you make a program sleep in C++ on Win 32?

How does one "pause" a program in C++ on Win 32, and what libraries must be included?
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
If you are using boost, you can use the thread::sleep function:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
If you wish for the program to stay responsive while "paused", you need to use a timer event.
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.