[英]C++ how to figure if a mutex is being disproportionately hogged by a single thread while blocking other threads
我想分析一個特定的互斥鎖是如何在兩個線程之間進行時間共享的。 我正在嘗試調試一個問題,我覺得其中一個線程正在非常迅速地鎖定/解鎖,這可能會使另一個線程無法獲取該鎖。
細節:
線程 T1:
while (1) {
//...non-blocking trivial work
lock();
//do little work on a shared DS1
unlock();
//...blocking work
}
線程 T2:
run() {
//...does a bunch of blocking work
lock();
// update shared DS1
unlock();
return;
}
線程 T1 是調度程序線程,如果共享的 DS1 已更新,則非常頻繁地執行緊密循環檢查。 線程 T2 做了很多工作,最后更新了共享的 DS1。
我覺得 T1 嚴重占用了鎖,同時讓 T2 無法足夠快地獲得鎖。
我想測量一個特定線程持有該鎖的時間。 T2 在爭奪鎖上花費了多少時間。
測量 T2 等待 lock() 調用返回的時間應該非常簡單:
// Thread T2
run() {
//...does a bunch of blocking work
// use whatever get-current-time API you like here
const uint64_t t1 = get_current_time_in_microseconds();
lock();
const uint64_t t2 = get_current_time_in_microseconds();
const uint64_t time_spent_waiting_for_lock = (t2-t1);
// Add code here to calculate minimum/average/max values
// of time_spent_waiting_for_lock over the course of your test
// update shared DS1
unlock();
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.