cpp实现系统睡眠机制

⌚Time: 2023-07-31 14:36:23

👨‍💻Author: Jack Ge

在系统中定义一个变量bHiberable,如果是3分钟内休眠,那么每隔3分钟检测一次这个变量,如果为真,则进入睡眠,如果是假,就把这个标志设置为真。继续等待和检测。
程序阻止系统休眠的办法:系统系统对外提供API,程序每调用一次API,就将bHiberable标志设置为假,可以阻止系统的休眠。

说明:我不知道真正的操作系统的睡眠机制。阻止Windows系统睡眠使用到windows.h的SetThreadExecutionState 系统API

一个系统类实现睡眠功能


class System{

public:

    System(){

        iWaitTimeMS = 3000;

    }

    static void do_not_hibernate(){

        bHiberable = false;

    }

    void start(){

        thread t(&System::try_hibernate, this);

        t.detach();

    }

    void try_hibernate(){

        while(1){

            this_thread::sleep_for(std::chrono::milliseconds(iWaitTimeMS));

            cout<<"System:尝试睡眠:";

            if(bHiberable){

                cout<<"进入睡眠状态。"<<endl;

                break;

            }else{

                cout<<"不能睡眠。"<<endl;

                bHiberable = true;

            }

        }

    }



    static bool bHiberable;

private:

    int iWaitTimeMS;

};

bool System::bHiberable = true;

程序类会在运行期间阻止系统睡眠


class Program123{

public:

    void run(){

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

    }

};

完整代码


#include <iostream>

#include <thread>



using namespace std;



class System{

public:

    System(){

        iWaitTimeMS = 3000;

    }

    static void do_not_hibernate(){

        bHiberable = false;

    }

    void start(){

        thread t(&System::try_hibernate, this);

        t.detach();

    }

    void try_hibernate(){

        while(1){

            this_thread::sleep_for(std::chrono::milliseconds(iWaitTimeMS));

            cout<<"System:尝试睡眠:";

            if(bHiberable){

                cout<<"进入睡眠状态。"<<endl;

                break;

            }else{

                cout<<"不能睡眠。"<<endl;

                bHiberable = true;

            }

        }

    }



    static bool bHiberable;

private:

    int iWaitTimeMS;

};

bool System::bHiberable = true;

class Program123{

public:

    void run(){

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

        this_thread::sleep_for(std::chrono::milliseconds(2000));

        cout<<"Program:阻止系统睡眠。"<<endl;

        System::do_not_hibernate();

    }

};

int main(){

    System s;

    s.start();



    Program123 p123;

    p123.run();



    cin.get();

    return 0;

}

运行测试