web123456

Summary of various sleep methods in C++ notes

Summary of C++ Notes on SLEEP

-- April 9, 2023 Shanghai


code review

Article Catalog

  • Summary of C++ Notes on SLEEP
    • ::this_thread::sleep_for()
      • be attached:std::this_thread::sleep_for(std::chrono::duration)
    • ::this_thread::sleep_until()
      • be attached:std::this_thread::sleep_until(std::chrono::time_point)
    • ::chrono::steady_clock::now()
    • 4.()
    • 4.()
    • 4. Answer: sleep() is a function of which library in C or C++?
    • The ros::Duration::sleep() in the
    • API - Sleep() function
    • Wait_for functions in ++
      • 7.::condition_variable::wait_for()
      • 7.::future::wait_for() or std::shared_future::wait_for()
    • ::this_thread::sleep(boost::posix_time::milliseconds(200));
    • ::posix_time
    • C++ environment sleep:QThread::sleep(num)

::this_thread::sleep_for()

This function belongs to the C++11 standard, it needs to include the header file <thread>, and when using it, you need to specify the length of time to sleep.
在这里插入图片描述

coding

#include <iostream>
#include <chrono>   // std::chrono::seconds
#include <thread>   // std::this_thread::sleep_for

int main() {
    std::cout << "Start sleeping..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Wake up!" << std::endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

be attached:std::this_thread::sleep_for(std::chrono::duration)

在这里插入图片描述

coding

#include <chrono>
#include <thread>

std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Pause for 100 milliseconds
  • 1
  • 2
  • 3
  • 4

::this_thread::sleep_until()

This function belongs to the C++11 standard, it needs to include the header file <thread>, and when using it, you need to specify the time point of sleep.
在这里插入图片描述

coding

#include <iostream>
#include <chrono>   // std::chrono::system_clock, std::chrono::seconds
#include <thread>   // std::this_thread::sleep_until

int main() {
    std::cout << "Start sleeping..." << std::endl;
    auto wake_up_time = std::chrono::system_clock::now() + std::chrono::seconds(1);
    std::this_thread::sleep_until(wake_up_time);
    std::cout << "Wake up!" << std::endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

be attached:std::this_thread::sleep_until(std::chrono::time_point)

This function is part of the C++11 standard and requires the inclusion of the header file <thread>, which suspends the current thread until a specified point in time.
在这里插入图片描述

coding

#include <chrono>
#include <thread>

auto now = std::chrono::system_clock::now();
auto end = now + std::chrono::seconds(10);
std::this_thread::sleep_until(end); // Pause 10 seconds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

::chrono::steady_clock::now()

This function is part of the C++11 standard and requires the inclusion of the header file <thread> to get the current point in time (which is not affected by changes in system time)
在这里插入图片描述

coding

#include <chrono>

auto start = std::chrono::steady_clock::now();
// Some operations
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = end - start;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.1.usleep()

usleep(): this function belongs to the POSIX standard, it needs to include the header file <>, and when using it, you need to specify the length of the sleep time in microseconds. However, this function has been deprecated and it is recommended to use std::this_thread::sleep_for() instead.
在这里插入图片描述

coding

#include <iostream>
#include <>   // usleep

int main() {
    std::cout << "Start sleeping..." << std::endl;
    usleep(1000000);   // Sleep 1 sec.
    std::cout << "Wake up!" << std::endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.()

<> The sleep function in the header file is used to hibernate the program for a specified number of seconds, i.e., to allow the program to pause execution until the specified time has elapsed.

To use the sleep function you need to include the <> header file and call the function sleep(unsigned int seconds) in your code. The seconds parameter indicates the number of seconds to put the program to sleep.
Note that the sleep function blocks the current thread, so if you need to do other things at the same time, consider using std::thread for multi-threaded programming.
在这里插入图片描述

coding

#include <>

int main() {
    // Program pause 5 seconds
    sleep(5);

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
'
(of a computer) run

4.3.ChatGPTAnswer: sleep() is a function of which library in C or C++?

In C/C++, thesleep() function depends on your operating system:

  1. Unix/Linux:: On Unix-like systems.sleep() Functions are defined in the<> in the header file. This function causes the program to pause execution for the specified number of seconds.

    #include <>
    
    int main() {
        unsigned int seconds = 5;
        sleep(seconds);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    '
    (of a computer) run
  2. Windows: InWindows in the system.Sleep() Functions are defined in the<> in the header file, note that it has 'S' capitalized. The argument to this function is milliseconds.

    #include <>
    
    int main() {
        DWORD milliseconds = 5000;
        Sleep(milliseconds);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

In standard C or C++, there is no definition of thesleep() Functions. These functions are platform-specific and are associated with the operating system's API. If you have a cross-platform program that requires the use of thesleep() functions, you may need to choose a different implementation depending on the compilation target or use cross-platform functions provided by third-party libraries such as Boost.

The ros::Duration::sleep() in the

Note that methods 1-4 are used to pause the execution of the current thread. If you need to pause the execution of the entire program, it is recommended to use the ros::Duration::sleep() method in ROS, as it will continue to process the ROS message loop during the pause.

In ROS, it is recommended to use the ros::Rate class to control the execution frequency of the loop, instead of using the sleep function to pause the program. ros::Rate can ensure that the program runs within a certain frequency, and automatically calculates and controls the sleep time, so as to achieve the purpose of precisely controlling the frequency of the loop.

In ROS, ros::Duration::sleep() is a function used to pause program execution for a period of time. It accepts a ros::Duration object representing the length of time for which the program is to be paused.

The following is an example where the program pauses for 1 second between posting two messages:
在这里插入图片描述

In this example, we use ros::Duration(1.0) to create a 1-second ros::Duration object and pass it to the sleep() method, which pauses the program for 1 second.
coding

#include "ros/"
#include "std_msgs/"

int main(int argc, char **argv) {
    ros::init(argc, argv, "example_node");
    ros::NodeHandle nh;

    ros::Publisher pub = nh.advertise<std_msgs::String>("example_topic", 10);

    while (ros::ok()) {
        std_msgs::String msg;
        msg.data = "Hello, world!";
        pub.publish(msg);

        ros::Duration(1.0).sleep(); // Pause 1 second

        msg.data = "Goodbye, world!";
        pub.publish(msg);

        ros::Duration(1.0).sleep(); // Pause 1 second
    }

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

API - Sleep() function

在这里插入图片描述

Wait_for functions in ++

7.::condition_variable::wait_for()

在这里插入图片描述

7.::future::wait_for() or std::shared_future::wait_for()

在这里插入图片描述

Both of the above wait_for methods can wait for some operation or condition for a certain period of time. However, their application scenarios and usage are different.

::this_thread::sleep(boost::posix_time::milliseconds(200));

在这里插入图片描述

::posix_time

boost::posix_time is part of the Boost.Date_Time library and provides a set of types and functions for representing times and dates. These types and functions are based on the POSIX time specification, hence the name "POSIX Time".

The Boost.Date_Time library provides a complete set of platform-independent date and time handling functions that enable C++ developers to work with dates and times on different platforms.

followingboost::posix_time Some of the commonly used types and functions in the

  1. ptime: represents a point in time. It consists of a date and a time interval.

    boost::posix_time::ptime t1(boost::posix_time::second_clock::local_time());
    
    • 1
  2. time_duration: represents a time interval.

    boost::posix_time::time_duration td(1, 10, 30);  // 1 hour, 10 minutes, 30 seconds
    
    • 1
  3. time_period: represents a time period consisting of two points in time.

    boost::posix_time::ptime t2 = t1 + boost::posix_time::hours(1);
    boost::posix_time::time_period tp(t1, t2);
    
    • 1
    • 2
  4. second_clock cap (a poem)microsec_clock: Provides a way to get the currentsystem timeThe methodology.

    boost::posix_time::ptime t3 = boost::posix_time::second_clock::local_time();   // Accurate to the second
    boost::posix_time::ptime t4 = boost::posix_time::microsec_clock::local_time(); // Precise to microseconds
    
    • 1
    • 2

These are just some of the basic types and functions.boost::posix_time The library also provides a set of functions to perform date and time operations such as addition, subtraction, comparison, etc. The library also supports parsing time from strings and formatting time to strings. It also supports parsing time from strings and formatting time to strings.

Note that starting with C++11, the C++ standard library has also begun to provide a complete set of date and time handling functions, namely<chrono> libraries. If your compiler supports C++11 or later, you may also want to consider using the<chrono> library to handle dates and times, rather than using theboost::posix_time

C++ environment sleep:QThread::sleep(num)

Qt'sQThread class provides three static functions to pause the current thread:

  • QThread::sleep(unsigned long secs): Pause the thread for a few seconds
  • QThread::msleep(unsigned long msecs): Suspend the thread for a few milliseconds
  • QThread::usleep(unsigned long usecs): Pause the thread for a few microseconds

These methods work directly on the current thread and are very easy to use, but note that they block the thread until the specified time has passed.

Sample code:

#include <QThread>
#include <QDebug>

void exampleFunction() {
    qDebug() << "Starting delay...";
    QThread::sleep(2);  // Sleep for 2 seconds
    qDebug() << "2 seconds passed.";
    QThread::msleep(500);  // Sleep for 500 milliseconds
    qDebug() << "500 milliseconds passed.";
}

int main() {
    exampleFunction();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15