tmp update

This commit is contained in:
张壹 2024-09-15 19:35:18 +08:00
parent d3abef6ac4
commit c760250dbe
3 changed files with 57 additions and 8 deletions

View File

@ -49,9 +49,6 @@ public:
set_end_process();
return;
}
private:
/* data */
};
int main(int argc, char const *argv[]) try

View File

@ -81,12 +81,13 @@ void gctl::process_monitor::start_monitoring()
return;
}
void gctl::process_monitor::wait_for_kerboard()
void gctl::process_monitor::wait_for_kerboard(int sec)
{
std::clog << "Press 'p' to pause. Continue in 1 seconds ..." << std::endl; // 键盘事件受到monitor_input的监控
// 键盘事件受到 keyboard_actions 的监控
std::cout << "Press 'p' to pause. Continue in " << sec << " seconds ..." << std::endl;
std::unique_lock<std::mutex> lck(mtx_); // 设置锁 等待监控函数的响应 如果超时则继续
if (cv_.wait_for(lck, std::chrono::seconds(1)) == std::cv_status::timeout)
if (cv_.wait_for(lck, std::chrono::seconds(sec)) == std::cv_status::timeout)
{
std::clog << "\x1b[1A\r\x1b[K";
}
@ -102,9 +103,12 @@ void gctl::process_monitor::keyboard_actions()
if (key_value == 112) // p的键值为112
{
std::unique_lock<std::mutex> lck(mtx_);
// 这里可以设置额外的条件和命令
std::cout << "\rInput 'q' to quit: ";
std::cin >> key_str;
// 执行更多的命令
if (key_str == "q") set_end_process(true);
cv_.notify_one();
}

View File

@ -42,19 +42,67 @@ namespace gctl
process_monitor();
virtual ~process_monitor();
/**
* @brief Implement the process that you want to be monitored here.
* This is just an API here. A template is shown as bellow:
*
* void process()
* {
* while (1)
* {
* // do something here
* ....
* ....
*
* wait_for_kerboard();
* if (end_process()) break;
* }
*
* set_end_process();
* return;
* }
*/
virtual void process() = 0;
/**
* @brief Start the monitoring process.
*
*/
virtual void start_monitoring();
virtual void wait_for_kerboard();
/**
* @brief Waiting the keyboard input to pause.
*
* @param sec wait time.
*/
virtual void wait_for_kerboard(int sec = 1);
/**
* @brief Implement excepted actions here when 'p' is pressed.
*
*/
virtual void keyboard_actions();
/**
* @brief Check to see if the process is set to end.
*
*/
virtual bool end_process();
/**
* @brief Set the end process object to true.
*
* @param ask Ask before setting the end process object to true.
*/
void set_end_process(bool ask = false);
private:
std::thread process_wapper()
{
return std::thread(&process_monitor::process, this);
}
private:
bool end_process_;
std::mutex mtx_;
std::condition_variable cv_;