C based timer: Difference between revisions

From SoftwareGuy
Jump to navigation Jump to search
Mark (talk | contribs)
Created page with "<code>#include <iostream> #include <unistd.h> <using namespace std; // Define the callback function void callback() { cout << "Timer callback called!" << endl;</code> <code>}</code> ''<code>// Create the timer</code>'' <code>timer_t timer;</code> <code>struct sigevent sigev;</code> ''<code>// Set up the timer</code>'' <code>sigev.sigev_notify = SIGEV_THREAD;</code> <code>sigev.sigev_notify_function = callback;</code> <code>sigev.sigev_value.sival_ptr = &timer;</c..."
 
Mark (talk | contribs)
No edit summary
Line 1: Line 1:
<code>#include <iostream>
<code>#include <iostream>
#include <unistd.h>
#include <unistd.h>
<using namespace std;
<using namespace std;</code>
// Define the callback function
 
void callback() {
// Define the callback function</code>
 
void callback() {</code>
 
cout << "Timer callback called!" << endl;</code>
cout << "Timer callback called!" << endl;</code>



Revision as of 14:22, 28 March 2024

#include <iostream>

  1. include <unistd.h>

<using namespace std;

// Define the callback function

void callback() {

cout << "Timer callback called!" << endl;

}

// Create the timer

timer_t timer;

struct sigevent sigev;

// Set up the timer

sigev.sigev_notify = SIGEV_THREAD;

sigev.sigev_notify_function = callback;

sigev.sigev_value.sival_ptr = &timer;

timer_create(CLOCK_REALTIME, &sigev, &timer);

// Start the timer

timer_settime(timer, 0, new itimerspec{1, 0}, NULL);

// Wait for the timer to expire

pause();

// Delete the timer

timer_delete(timer);