// Detaches the thread represented by the object from the calling thread, // allowing them to execute independently from each other. // Both threads continue without blocking nor synchronizing in any way. Note // that when either one ends execution, its resources are released. // After a call to this function, the thread object becomes non-joinable and // can be destroyed safely. t1.detach(); t2.detach(); }
voidadd_to_list(int new_value) { { // lock std::lock_guard<std::mutex> guard(some_mutex); some_list.push_back(new_value); // lock_guard out of the range will unlock } std::cout << "Unlock" << std::endl; }
boollist_contains(int value_to_find) { { // lock std::lock_guard<std::mutex> guard(some_mutex); return std::find(some_list.begin(), some_list.end(), value_to_find) != some_list.end(); // lock_guard out of the range will unlock } std::cout << "Unlock" << std::endl; }
/* unique_lock have more lock operation */ // guard.lock(); // guard.unlock(); // guard.try_lock();
return std::find(some_list.begin(), some_list.end(), value_to_find) != some_list.end(); // lock_guard out of the range will unlock } std::cout << "Unlock" << std::endl; }
voidProducerThread(){ // Create the producer thread and detach it. std::thread producer_thread(&Threads::ProducerTask, this); producer_thread.detach(); }
voidCustomerThread(){ // Create the customer thread and detach it. std::thread customer_thread(&Threads::CustomerTask, this); customer_thread.detach(); }
voidProducerTask(){ while (true) { HandleProducerTask();
voidCustomerTask(){ while (true) { // Get the lock status. std::unique_lock<std::mutex> lk(cv_mutex_); // Wait the condition variable status. cv_.wait(lk, [] { return (data_cache.size() % 2 == 0 && data_cache.size() != 0) ? true : false; }); // If the wait condition variables is false then the wait // will auto free the lock and block the thread. // If the wait condition variables is true then the thread // can have the lock and it will go the flower logic. HandleCustomerTask(); // The wait will have the lock, so need free it by the handle for to // other thread use. lk.unlock(); } } voidHandleCustomerTask(){ std::cout << "-----------------------" << std::endl; while (!data_cache.empty()) { std::cout << data_cache.front() << std::endl; data_cache.pop(); } } private: std::condition_variable cv_; mutable std::mutex cv_mutex_; };
intmain(int argc, charconst *argv[]) { // Normal async task. AsyncTask async_task; int a = 3; int b = 4; int c = 0;
// The async is run a async_task point p->add() in the current thread. auto f1 = std::async(&AsyncTask::Add, &async_task, a, b, std::ref(c));
// The async is run a async_task copy new object new_async_task.del() in the current thread. auto f2 = std::async(&AsyncTask::Del, async_task, a, b, std::ref(c));
// The async is run a async_task copy new object new_async_task() in the current thread. auto f3 = std::async(AsyncTask(), a, b, std::ref(c));
// The async is run the async_task async_task object async_task() in the current thread. auto f4 = std::async(std::ref(async_task), a, b, std::ref(c));
// The async is run a async_task copy new object new_async_task() in create a new thread. auto f5 = std::async(std::launch::async, AsyncTask(), a, b, std::ref(c));
// f5.get(); f5.wait(); std::cout << c << std::endl;
// The async is run the async_task async_task object async_task() in the current thread. auto f6 = std::async(std::launch::deferred, AsyncTask(), a, b, std::ref(c)); // The async is run the async_task async_task object async_task() will have two choice. auto f7 = std::async(std::launch::async | std::launch::deferred, AsyncTask(), a, b, std::ref(c));
// f1.get(); f1.wait(); std::cout << c << std::endl; // f2.get(); f2.wait(); std::cout << c << std::endl;
// f3.get(); f3.wait(); std::cout << c << std::endl;
// f4.get(); f4.wait(); std::cout << c << std::endl;
// f6.get(); f6.wait(); std::cout << c << std::endl;
// f7.get(); f7.wait(); std::cout << c << std::endl;
intmain(int argc, charconst *argv[]) { std::promise<int> my_pri; std::thread work1([](std::promise<int>& p){ // Sleep 10s. std::this_thread::sleep_for(std::chrono::seconds(2)); try { p.set_value(32); } catch(...) { // Throw the system error p.set_exception(std::current_exception()); // Throw the custom error p.set_exception(std::make_exception_ptr(std::logic_error("The value is less than zero."))); } }, std::ref(my_pri)); // Need run the thread. work1.detach();
// The share_future can be copy then can pass to other thread and // the other can to wait the result to count. std::shared_future<int> sf = my_pri.get_future().share();
std::thread work2([] (std::shared_future<int> f) { try { // The share_future can by run more times. std::cout << "Work2 " << f.get() << std::endl; } catch(...) { std::cout << "Work2 Wait error" << std::endl; } }, sf); work2.detach();
std::thread work3([] (std::shared_future<int> f) { try { // The share_future can by run more times. std::cout << "Work3 " << f.get() << std::endl; } catch(...) { std::cout << "Work3 Wait error" << std::endl; } }, sf); work3.detach();
//------ future ---------- // By the future only run one times and only one by other thread run get function. std::future<int> my_future = my_pri.get_future();
// Get the thread return value std::cout << "Main get" << my_future.get() << std::endl;
intSeeHello(unsignedint time){ // When sleep time, it will wait the function SeeHello end. std::this_thread::sleep_for(std::chrono::seconds(time)); return1; }
template<typename T> voidShow(T val){ std::cout << "Get the val: " << val << std::endl; }
intmain(int argc, charconst *argv[]) { unsignedint time = 3; std::future<int> f1 = std::async(std::launch::async, SeeHello, time);
voidwait_loop() { is_time_out_flag_ = false; // Set the out fo time unsignedint set_time = 5; autoconst timeout = std::chrono::steady_clock::now() + std::chrono::seconds(set_time); std::unique_lock<std::mutex> lk(m_); // Lock and int the loop to wait the time out // If the flag is true then exit while(!is_time_out_flag_) { if (cv_.wait_until(lk, timeout) == std::cv_status::timeout) { break; // return the false flag. } } return; }