1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HDC_RUST_CTIMER_H 16 #define HDC_RUST_CTIMER_H 17 #include <iostream> 18 #include <string> 19 #include <thread> 20 #include <chrono> 21 #include <atomic> 22 #include <memory> 23 #include <condition_variable> 24 #include <functional> 25 26 class CTimer { 27 public: 28 template<class F> CTimer(F func)29 explicit CTimer(F func) 30 { 31 this->func = func; 32 } ~CTimer()33 virtual ~CTimer() {} 34 void Start(unsigned int imsec, bool immediatelyRun = false); 35 void Stop(); 36 void SetExit(bool exit); 37 private: 38 void Run(); 39 private: 40 std::atomic_bool exit = false; 41 std::atomic_bool immediatelyRun = false; 42 unsigned int msec = 1000; 43 std::function<void(void)> func; 44 std::thread thread; 45 std::mutex mutex; 46 std::condition_variable cond; 47 }; 48 #endif 49