Member functions
(constructor) constructs the object
(destructor) destructs the object
operator=[deleted] not copy-assignable
Notification
notify_one notifies one waiting thread
notify_all notifies all waiting threads
Waiting
wait blocks the current thread until the condition variable is woken up
wait_for blocks the current thread until the condition variable is woken up or after the specified timeout
duration
wait_until blocks the current thread until the condition variable is woken up or until specified time point has been reached
Native handle
native_handle returns the native handle.
#include <condition_variable>
using namespace std::placeholders;
std::condition_variable m_condVar;
// Make This Thread sleep for 1 Second
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout<<"Loading Data from XML"<<std::endl;
// Lock The Data structure
std::lock_guard<std::mutex> guard(m_mutex);
// Set the flag to true, means data is loaded
// Notify the condition variable
std::cout<<"Do Some Handshaking"<<std::endl;
std::unique_lock<std::mutex> mlock(m_mutex);
// Start waiting for the Condition Variable to get signaled
// Wait() will internally release the lock and make the thread to block
// As soon as condition variable get signaled, resume the thread and
// again acquire the lock. Then check if condition is met or not
// If condition is met then continue else again go in wait.
m_condVar.wait(mlock, std::bind(&Application::isDataLoaded, this));
std::cout<<"Do Processing On loaded Data"<<std::endl;
std::thread thread_1(&Application::mainTask, &app);
std::thread thread_2(&Application::loadData, &app);
Comments