answersLogoWhite

0


Best Answer

Unfortunately, there is no cross-platform method of forcing a delay. Microsoft uses the sleep function (with a 1 second resolution) while UNIX uses usleep (with 1 nanosecond resolution). However, neither method can be regarded as being accurate in a multi-processing environment -- both will sleep for "at least" the given time, but if the system is busy your application will continue to sleep until its next time-slice comes around.

Although usleep has a resolution of 1 nanosecond (1 billionth of a second), the system's frequency determines the actual resolution. For instance if your system has a frequency of 25 million ticks per second, your resolution is only 25 microseconds, so you cannot sleep for any less than that. Plus you have to add on the time it takes to make the call.

C++11 provides much better support for hi-resolution timers in the <chrono> header, so at least we now have a portable method of sleeping for a preset duration. However the duration is still "at least" -- background tasks can still prevent your program from waking up on time.

The following example demonstrates how to print the current time and how to put your program to sleep for a duration. Note that we still have no portable method of obtaining the current time in a thread-safe manner -- but the code includes a portable workaround.

#include<iostream> // std::cout, std::endl

#include<iomanip> // std::put_time

#include<sstream> // std::stringstream

#include<chrono> // std::chrono (C++11)

#include<thread> // std::this_thread (C++11)

namespace my_std

{

// std::localtime is not thread-safe but there is

// no portable alternative in the standard. Thus

// we must define our own standards...

tm localtime (const std::time_t& time)

{

std::tm snapshot;

#if (defined (WIN32) defined (_WIN32) defined (__WIN32__))

localtime_s (&snapshot, &time); // Microsoft-specific

#else

localtime_r (&time, &snapshot); // POSIX

#endif

return snapshot;

}

};

// Returns the current time and date as a string.

std::string current_time_and_date()

{

std::chrono::system_clock::time_point now = std::chrono::system_clock::now();

time_t as_time_t = std::chrono::system_clock::to_time_t (now);

std::stringstream ss;

ss << std::put_time (&my_std::localtime (as_time_t), "%Y-%m-%d %X");

return ss.str();

}

int main()

{

std::cout << "The current time is: " << current_time_and_date() << std::endl;

std::cout << "Taking a nap for half-a-second..." << std::endl;

std::this_thread::sleep_for (std::chrono::milliseconds (500)); // half-a-second

std::cout << "The current time is: " << current_time_and_date() << std::endl;

// 5-second ticker...

std::chrono::system_clock::time_point now = std::chrono::system_clock::now();

for (unsigned tick=0; tick<5; ++tick)

{

std::this_thread::sleep_until (now + std::chrono::seconds (tick));

std::cout << "Tick..." << std::endl;

}

std::this_thread::sleep_until (

now + std::chrono::seconds (5));

std::cout << "BOOM!" << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Sample code of c plus plus by using delay?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which country is using the code plus 44?

The UK


What is a sample code for C plus plus?

Copy and paste this and it will say hello world! #include &lt;iostream&gt; int main() { std: :cout &lt; &lt; "Hello, world!\n ";


Sample programs in c plus plus language using pointers?

#include int main (int argc, char **argv){int i;for (i=0; i


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


What are general debugging methods in c plus plus?

General debugging techniques include using exceptions and using the debugger to step-into certain sections of code to check for errors.


C plus plus code to find the length of a string without using function?

int i = 0; while(str[i] != NULL){ i++; }


How do you convert a turbo c plus plus code into Linux shell scripting code without using gcc or g plus plus compiler?

You cannot. C++ and shell script (which shell, by the way? there are more than one) are entirely different languages.


How do you display the square root symbol on output screen using C Plus Plus?

The the square root symbol can be displayed by using ASCII code cout&lt;&lt;"press alt251"; pressing alt251 "&#65533;&#65533;"


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


How do you display the date and time of execution using turbo c plus plus?

Please use this Sample Code given below : Source Code: #include&lt;iostream.h&gt; #include&lt;conio.h&gt; #include&lt;dos.h&gt; void main() { clrscr(); struct date d; int dd,mm,yy; getdate(&amp;d); dd=d.da_day; mm=d.da_mon; yy=d.da_year; cout&lt;&lt;"\n\n\n\t\t\tDATE : "&lt;&lt;dd&lt;&lt;"/"&lt;&lt;mm&lt;&lt;"/"&lt;&lt;yy; getch(); } Any queries can checked using the help index in c/c++.


Which mobile company code is plus plus 372?

Its code of estonia


Plus 621 is for which country code?

plus 621 what country code