#include <iostream>
#include <thread>
#include <string>
#include <mutex>
using namespace std;
mutex m1;
int var=1;


void function(int id)
{
    //m1.lock();
    for(int i=0;i<5;i++)
    {
        //m1.lock();
       cout<<"Thread "<<id<<" is running for "<<i<<" for value : "<<var<<endl;  
        var++;
       //m1.unlock();
    }
   // m1.unlock();
    
}


int main() {

    thread t1(function,1);
    thread t2(function,2);
    
    t1.join();
    t2.join();
    
    return 0;
}

/*
void printsafe(const string &message)
{
    m1.lock();
    cout<<message<<endl;
    m1.unlock();
}

void function(int id)
{
    for(int i=0;i<5;i++)
    {
       printsafe("Thread "+to_string(id)+"is running for "+to_string(i));     
    }
}
*/

Embed on website

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