#include <iostream>
#include <mutex>
#include <thread>
#include <chrono>
using namespace std;
mutex m;
int value=0;

void function(int c)
{
    if(m.try_lock_for(std::chrono::seconds()))
    {
        m.lock();
        std::this_thread::sleep_for(std::chrono::seconds());
        value++;
        cout<<"Thread "<<c<<" entered : "<<value<<endl;
        m.unlock();
    }
    else
    {
        cout<<"Thread "<<c<<" not entered : "<<value<<endl;
    }
    
}


int main() {

    thread t1(function,1);
    thread t2(function,2);

    t1.join();
    t2.join();
    
    return 0;
}

Embed on website

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