Re: [問題] 關於thread釋放記憶體問題

看板C_and_CPP作者 (Kevin)時間15年前 (2009/03/05 21:07), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《puph (真冷...)》之銘言: : 大家好 小弟目前在寫一個C code : 需要用到timer timer會由事件處發 : 我使用thread來實現timer 每當事件發生就起始一個thread : 我想要計時 5秒 thread一啟動 就先下sleep(5) : 等sleep結束 再執行我想要做的事 : 做完下pthread_exit() 結束thread : 以上 : 每一個時間點 大概會有2~3個thread被執行中 : 不過很詭異的事情 當我執行一段時間後 : 就會顯示 "Cannot allocate memory" : 想請問 有確實的下了pthread_exit() 為何還會出現記憶體空間不足? 線程可用pthread_exit(void *retval)結束 但是並不會清理資源 直到你在其他任何線程中呼叫pthread_join(pthread_t thread_id, void** ret); thread_id是pthread_create時產生 *ret=retval 或者 將該線程用pthread_detach(thread_id)設為detached模式 return之後該線程的資源會被自動釋放 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.136.181.131

03/05 21:20, , 1F
原來是這個原因 感謝你 明天馬上來試試看!!
03/05 21:20, 1F

03/05 21:43, , 2F
ret可以丟NULL進去
03/05 21:43, 2F
文章代碼(AID): #19hywLwL (C_and_CPP)
文章代碼(AID): #19hywLwL (C_and_CPP)