#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
mutex m;
condition_variable cv;
int balance=0;

void deposit_money(int money)
{
    std::lock_gaurd<mutex> lg(m);
       balance+=money;
    cout<<"Balance after Deposit : "<<balance<<endl;
    cv.notify();
}


void withdraw_money(int money)
{
    std::unique_lock<mutex> ul(m);
    cv.wait(ul ,[]{return (balance!=0)? true:false;});
     if(money<=balance)
     {
         balance = balance-money;
         cout<<"balance after withdrawl : "<<balance<<endl;
     }
     else
     {
         cout<<"balance cant be withdrawl : "<<balance<<endl;
     }

    cout<<"Current Balance is : "<<balance<<endl;
    
}


int main() {

    thread t1(deposit_money,500);
    thread t2(withdraw_money,500);

    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: