看板 C_and_CPP 關於我們 聯絡資訊
Pthread有暫停suspend和resume功能嗎 因為我需要讓Thread在執行到一半時暫停 像是 Work Thread Exec Body { for( i = 1~100 ) { call_action_1(); call_action_2(); call_action_3(); } } Another Thread { pthread_suspend(); if( cond == true ) pthread_resume(); } another thread執行了pthread_suspend work thread就放下執行工作停下 可能停在任意的一行code上, 有可能是action1或者action3 不曉得Linux上的PThread有沒有支援 我查google 和 API好像都沒這兩個功能 不曉得有什麼方式可以達到 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.216.117 littleboypis:轉錄至看板 Programming 03/08 16:43
tinlans:有些系統會提供非標準的函式 (_np 結尾的 pthread 函式)。 03/08 16:52
tinlans:譬如 FreeBSD 有 pthread_suspend_np, pthread_resume_np 03/08 16:54
tinlans:不過有不少論點和實作經驗證實這樣設計程式很危險。 03/08 17:10
softwind:用condition去擋吧 會需要突然去擋一個thread嗎??? 03/08 20:37
littleboypis:回softwind大,真的有此需要 得突然暫停某個Thread 03/08 20:42
tinlans:一般不建議這麼做的理由就是你很難判斷你 suspend 的 03/08 21:22
tinlans:thread 目前到底在幹什麼,或是做某件事做到什麼程度了。 03/08 21:22
tinlans:比方說正在修改 linked list 的 pointer 到一半被停住,然 03/08 21:23
tinlans:後那個 linked list 又馬上被拿來存取。 03/08 21:23
jlovet:喔喔,我好像認識你,你可以用 pthread_cond_wait 03/09 11:52
jlovet:他是要暫停自己這個thread的話就沒問題,但是不能中斷別人 03/09 13:42