TOPIC: Condition Variable In C++ Threading
NOTES:
1. Condition variables allow us to synchronize threads via notifications.
a. notify_one();
b. notify_all();
2. You need mutex to use condition variable
3. Condition variable is used to synchronize two or more threads.
4. Best use case of condition variable is Producer/Consumer problem.
5. Condition variables can be used for two purposes:
a. Notify other threads
b. Wait for some condition
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int myAmount = 0;
std::mutex m;
void addMoney() {
m.lock();
++myAmount;
m.unlock();
}
int main() {
std::thread t1(addMoney);
std::thread t2(addMoney);
t1.join();
t2.join();
cout << myAmount << endl;
return 0;
}
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int myAmount = 0;
std::mutex m;
void addMoney() {
std::lock_gaurd<std::mutex> lock(m);
++myAmount;
}
int main() {
std::thread t1(addMoney);
std::thread t2(addMoney);
t1.join();
t2.join();
cout << myAmount << endl;
return 0;
}
2 Comments
wrong code given here
ReplyDelete#include
ReplyDelete#include
#include
#include
using namespace std;
int balance = 0;
std::mutex m;
std::condition_variable cv;
void addMoney(int money ) {
std::lock_guard lg(m);
balance+=500;
cout<<"addMoney current Balance:" << balance< ul(m);
cv.wait(ul, [](){return (balance!=0)?true:false;});
if(balance>=money){
balance-=money;
cout<<"Amount deducted:" << money<<endl;
}else{
cout<<"amount can't be deducted, current balances is less than :" << money<<endl;
}
cout<<"withdrawMoney current Balance:" << balance<<endl;
cv.notify_one();
}
int main() {
std::thread t1(withdrawMoney, 500);
std::thread t2(addMoney, 500);
t1.join();
t2.join();
return 0;
}