// Sistemas Operacionais
// Cássio Pinheiro
// Sincronizando Tarefas com Mensagem

#include <iostream>
#include <thread>

using namespace std;

class mensagem {
	private:
		string sBuffer;
		thread::id nId;
	public:
		mensagem( void ) { sBuffer = ""; }
		void nova( thread::id id, string msg ) { sBuffer = msg; nId = id; }
		string msg( void ) { return( sBuffer ); }
		thread::id id( void ) { return( nId ); }
} oBuffer;

void thr1( void ) {
    cout << "Iniciando 1" << endl;
    while( true ) {
		if( oBuffer.msg() == "vai" ) cout << "Msg: '" << oBuffer.msg() << "' de: " << oBuffer.id() << endl;
		if( oBuffer.msg() == "fim" ) break;
	}
	cout << "Msg: '" << oBuffer.msg() << "' de: " << oBuffer.id() << endl;
    cout << "Fim 1" << endl;
}

void thr2( void ) {
    cout << "Iniciando 2" << endl;
    this_thread::sleep_for( chrono::milliseconds( 1000 )); 
    oBuffer.nova( this_thread::get_id(), "vai" );
    this_thread::sleep_for( chrono::milliseconds( 1 )); 
    cout << "Fim 2" << endl;
    oBuffer.nova( this_thread::get_id(), "fim" );
}

int main() {
    thread t1( thr1 );
    thread t2( thr2 );
    t1.join();
    t2.join();
    return( 0 );
}

Embed on website

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