#include <iostream>
#include <thread>
using namespace std;
#include<mutex>
std::mutex m1;
int count =0;

/*
void func()  // lock
{   
    m1.lock();
    count++;
    m1.unlock();
   }
}
*/

/*
void func()     //try lock
{
   for(int i=0;i<100000;i++)
   { 
    if(m1.try_lock())
    {
    count++;
    m1.unlock();
    }
   }
}
*/

int main() 
{

    std :: thread t1(func);
    std :: thread t2(func);

    t1.join();
    t2.join();

    cout<<"count : "<<count<<endl;
    
    return 0;
}

Embed on website

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