gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
你当前是什么办法
- User
你当前是什么办法
- Assistant
我是一个人工智能语音助手,我没有身体,只存在于语音助手平台上,通过语音和文字与用户进行交互。
- User
你当前是什么版本
- Assistant
作为语音助手,我没有具体的版本号,因为我是一个基于云的服务。经常会进行更新和改进来提升我的功能和性能,但我不会有稳定的版本号。
- User
帮我写一个高精度的C++定时器程序
- Assistant
以下是一个简单的高精度定时器示例程序,用C++编写:
#include <iostream> #include <chrono> // 包含时间库chrono #include <thread> // 包含线程库thread class Timer { public: Timer() : m_duration(0), m_status(false) {} ~Timer() { stop(); } bool isRunning() const { return m_status; } void start(std::chrono::milliseconds duration, bool repeat = true) { if (m_status) { stop(); } m_duration = duration; m_repeat = repeat; m_status = true; m_thread = std::thread(&Timer::run, this); } void stop() { if (m_status) { m_status = false; if (m_thread.joinable()) { m_thread.join(); } } } void run() { auto startTime = std::chrono::system_clock::now(); while (m_status) { auto endTime = std::chrono::system_clock::now(); auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime); if (elapsedTime >= m_duration) { startTime = std::chrono::system_clock::now(); if (m_repeat) { onTimer(); } else { m_status = false; } } } } virtual void onTimer() = 0; private: std::chrono::milliseconds m_duration; bool m_repeat; bool m_status; std::thread m_thread; }; class MyTimer : public Timer { public: virtual void onTimer() override { std::cout << "Timer expired!" << std::endl; } }; int main() { MyTimer timer; timer.start(std::chrono::milliseconds(500)); // 设置定时器时间为500毫秒 std::this_thread::sleep_for(std::chrono::seconds(5)); timer.stop(); // 停止定时器 return 0; }