#include <iostream>
#include <mutex>
#include <thread>
#include <unistd.h>
using namespace std;
mutex mtx;
void function( int num) {
cout << "waiting for the lock in thread " << this_thread:: get_id ( ) << endl;
lock_guard< mutex> lock( mtx) ;
sleep( 2 ) ;
cout << "Acquired the lock in thread " << this_thread:: get_id ( ) << endl;
return ;
}
int main( ) {
thread t1( function, 5 ) ;
thread t2( function, 5 ) ;
t1.join ( ) ;
t2.join ( ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bXV0ZXg+CiNpbmNsdWRlIDx0aHJlYWQ+CiNpbmNsdWRlIDx1bmlzdGQuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgptdXRleCBtdHg7Cgp2b2lkIGZ1bmN0aW9uKGludCBudW0pIHsKICAgIGNvdXQgPDwgIndhaXRpbmcgZm9yIHRoZSBsb2NrIGluIHRocmVhZCAiIDw8IHRoaXNfdGhyZWFkOjpnZXRfaWQoKSA8PCBlbmRsOwogICAgbG9ja19ndWFyZDxtdXRleD4gbG9jayhtdHgpOwogICAgc2xlZXAoMik7CiAgICBjb3V0IDw8ICJBY3F1aXJlZCB0aGUgbG9jayBpbiB0aHJlYWQgIiA8PCB0aGlzX3RocmVhZDo6Z2V0X2lkKCkgPDwgZW5kbDsKICAgIHJldHVybjsKfQoKaW50IG1haW4oKSB7CiAgICB0aHJlYWQgdDEoZnVuY3Rpb24sIDUpOwogICAgdGhyZWFkIHQyKGZ1bmN0aW9uLCA1KTsKCiAgICB0MS5qb2luKCk7CiAgICB0Mi5qb2luKCk7CiAgICByZXR1cm4gMDsKfQ==