Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4919

C/C++ • Re: Timers in C++

$
0
0
Maybe something like this could work for you:

Code:

#include <iostream>#include <chrono>#include <thread>int main(){  std::thread timer([]() {    std::this_thread::sleep_for(std::chrono::seconds(2));    std::cout << "Turning ON" << std::endl;    std::this_thread::sleep_for(std::chrono::seconds(4));    std::cout << "Turning OFF" << std::endl;  });  std::cout << "Timer started. Will fire in 2 seconds." << std::endl;  // Wait for timer to exit  timer.join();  return 0;}
To compile it:

Code:

g++ -o timer -pthread -l pthread timer.cpp

Statistics: Posted by deepo — Tue Dec 10, 2024 10:28 pm



Viewing all articles
Browse latest Browse all 4919

Trending Articles