// Sistemas Operacionais
// Cássio Pinheiro
// Clock com Thread

#include <iostream>
#include <thread>
#include <chrono>

#define FREQ 16

using namespace std;

class cClock {
	private:
		bool bNivel;
	public:
		cClock( void ) { bNivel = true; }
		void update( void ) { bNivel = ! bNivel; }
		bool nivel( void ) { return( bNivel ); }
} oClock;

void delay_msegs( unsigned milisegs ) { this_thread::sleep_for( chrono::milliseconds( milisegs ) ); }

bool pulso( void ) { while( true ) { oClock.update(); delay_msegs( 1000 / FREQ ); } }

int main() {
	thread tp( pulso );
    int nPulso = 1;
    long nItera = 1;
    bool bMudou = true;
    while( nPulso <= 10 ) {
        if( oClock.nivel() ) {
            if( bMudou ) {
                bMudou = false;
                cout << "Iteração: " << nItera << " Pulso: " << nPulso++ << endl;
            }
        }
        else bMudou = true;
        nItera++;
    }
    return( 0 );
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: