條件變數wait

2021-10-10 22:40:40 字數 3975 閱讀 7222

(1) wait(lock): 呼叫時即阻塞執行緒,並且呼叫lock.unlock() 

(2) wait(lock, conditions): 呼叫時檢查conditions,如果為false,則阻塞執行緒,並且呼叫lock.unlock(), 否則,繼續執行、

(3) wait_for(lock, time_duration, conditions): 呼叫時,檢查條件是否滿足:(1) conditions返回true; (2) 時間超時,如果不滿足(1)(2)中的乙個條件,則阻塞執行緒,並呼叫lock.unlock(), 否則,到達一定等待時間或滿足條件被喚醒 ,注意,等待超過時間段後自動喚醒,判斷條件一般需要使用者自己在合適的時候判斷,並通過notify_one()或notify_all()喚醒,所以,使用的時候注意判斷返回值,即狀態是否為std::cv_status::timeout

(4) wait_until(lock, time_point, conditions): 實際wait_for是通過wait_until實現,實際上也是一樣的,到達指定時間點或滿足條件conditions時被喚醒,注意,到達時間點是自動喚醒,判斷條件一般需要使用者自己在合適的時候判斷,並通過notify_one()或notify_all()喚醒,所以,使用的時候注意判斷返回值,即狀態是否為std::cv_status::timeout

#include #include #include #include #include using namespace std;

static std::mutex mtx;

static bool ready = false;

static std::condition_variable ***;

static int block_time = 20;

void run_thread(int i)

#else

std::cout << __function__ << ": run here wait start i = " << i << std::endl;

***.wait(lg, ); // 如果條件滿足,則執行,否則,直接阻塞,釋放鎖

std::cout << __function__ << ": run here wait i = " << i << std::endl;

#endif

std::cout << __function__ << ": run here ready i = " << i << std::endl;

}void run_thread2(int i)

); // 如果條件滿足,則執行,否則,直接阻塞,釋放鎖

std::cout << __function__ << ": run here wait i = " << i << ", ready = " << ready << ", currenttime = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

std::cout << __function__ << ": run here ready i = " << i << ", ready = " << ready << ", currenttime = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

}void run_thread3(int i)

); // 如果條件滿足,則執行,否則,直接阻塞,釋放鎖

std::cout << __function__ << ": run here wait i = " << i << ", ready = " << ready << ", currenttime = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

std::cout << __function__ << ": run here ready i = " << i << ", ready = " << ready << ", currenttime = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

}void run_go()

int main()

std::cout << "currenttime b = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

std::cout << "run here create thread end" << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(block_time));

std::cout << "run here create thread wait " << std::endl;

std::cout << "currenttime c = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

// run_go();

for(int i = 0; i < 10; ++i)

}std::cout << "***************= case 2 end ***************=" << std::endl;

}std::cout << "currenttime b = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

std::cout << "run here create thread end" << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(block_time));

std::cout << "run here create thread wait " << std::endl;

std::cout << "currenttime c = " << std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch().count() << std::endl;

// // run_go();

for(int i = 0; i < 10; ++i)

}std::cout << "***************= case 3 end ***************=" << std::endl;

}std::cout << "run here create thread end" << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(block_time));

std::cout << "run here create thread wait " << std::endl;

run_go();

for(int i = 0; i < 10; ++i)

}std::cout << "***************= case 1 end ***************=" << std::endl;

}return 0;

}

條件變數 pthread cond init

include int pthread cond init pthread cond t cv,const pthread condattr t cattr 返回值 函式成功返回0 任何其他返回值都表示錯誤初始化乙個條件變數。當引數cattr為空指標時,函式建立的是乙個預設的條件變數。否則條件變數的...

條件變數 pthread cond init

include int pthread cond init pthread cond t cv,const pthread condattr t cattr 返回值 函式成功返回0 任何其他返回值都表示錯誤初始化乙個條件變數。當引數cattr為空指標時,函式建立的是乙個預設的條件變數。否則條件變數的...

pthread cond t條件變數

條件變數是利用執行緒間共享的全域性變數進行同步的一種機制,主要包括兩個動作 乙個執行緒等待 條件變數的條件成立 而掛起 另乙個執行緒使 條件成立 給出條件成立訊號 為了防止競爭,條件變數的使用總是和乙個互斥鎖結合在一起。一 pthread cond wait定義 函式原型 int pthread c...