I cannot really understand this behavior. Take a look at this piece of code:
<code>#include <iostream>
#include <string>
#include <condition_variable>
std::condition_variable cond_var;
std::mutex mtx;
int gi = 0;
class Math
{
public:
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++mi < 100000);
cond_var.notify_one();
}
int mi = 0;
};
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++gi < 100000);
cond_var.notify_one();
}
int main(int argc, char** argv)
{
std::thread th(Sum);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[] {
return gi >= 100000;
});
}
std::cout << "i = " << gi << 'n';
std::cin.get();
return 0;
}
/*int main(int argc, char** argv)
{
Math math;
std::thread th(&Math::Sum, &math);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[math] {
return math.mi >= 100000;
});
}
std::cout << "Math.mi = " << math.mi << 'n';
std::cin.get();
return 0;
}*/
</code>
<code>#include <iostream>
#include <string>
#include <condition_variable>
std::condition_variable cond_var;
std::mutex mtx;
int gi = 0;
class Math
{
public:
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++mi < 100000);
cond_var.notify_one();
}
int mi = 0;
};
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++gi < 100000);
cond_var.notify_one();
}
int main(int argc, char** argv)
{
std::thread th(Sum);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[] {
return gi >= 100000;
});
}
std::cout << "i = " << gi << 'n';
std::cin.get();
return 0;
}
/*int main(int argc, char** argv)
{
Math math;
std::thread th(&Math::Sum, &math);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[math] {
return math.mi >= 100000;
});
}
std::cout << "Math.mi = " << math.mi << 'n';
std::cin.get();
return 0;
}*/
</code>
#include <iostream>
#include <string>
#include <condition_variable>
std::condition_variable cond_var;
std::mutex mtx;
int gi = 0;
class Math
{
public:
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++mi < 100000);
cond_var.notify_one();
}
int mi = 0;
};
void Sum()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lck(mtx);
while (++gi < 100000);
cond_var.notify_one();
}
int main(int argc, char** argv)
{
std::thread th(Sum);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[] {
return gi >= 100000;
});
}
std::cout << "i = " << gi << 'n';
std::cin.get();
return 0;
}
/*int main(int argc, char** argv)
{
Math math;
std::thread th(&Math::Sum, &math);
std::cout << "Waiting...n";
{
std::unique_lock<std::mutex> lck(mtx);
cond_var.wait(lck,
[math] {
return math.mi >= 100000;
});
}
std::cout << "Math.mi = " << math.mi << 'n';
std::cin.get();
return 0;
}*/
The first main function works perfectly fine: the main thread waits until Sum notifies it when its work is completed.
The second main function, however, does not work. I placed a breakpoint at the cond_var.notify_one() line in the Sum member of Math and that line is definitely hit, but it the predicate of wait is never called. What am I doing wrong here?