在linux/list.h
評論它是寫的是:linux/list.h - 如何從列表中安全刪除項目?
- 使用
list_del_entry
:注:list_empty
在進入不返回後這個真實的,該項處於不確定狀態。 - 對於
list_del
:這僅適用於內部列表操作,我們已知道上一個/下一個條目!
所以,我將如何安全地從鏈表中刪除對象,並確保list_empty
是功能性的,或者確保下一個鏈表結點的刪除是正確的?
這是我目前實施:
struct kool_list{
int to;
struct list_head list;
int from;
};
struct kool_list *tmp;
struct list_head *pos, *q;
struct kool_list mylist;
list_for_each_safe(pos, q, &mylist.list){
tmp= list_entry(pos, struct kool_list, list);
printf("freeing item to= %d from= %d\n", tmp->to, tmp->from);
list_del(pos);
free(tmp);
}
如果列表可以在兩個上下文中使用,則需要使用鎖定。符號'_safe()'似乎是安全的,只能用於正向的列表遍歷(閱讀)。一些'archs'可以被無鎖寫入,但Linux尚未做到這一點。 (這可能是已知的,但我認爲這是值得一提的)。 – 2013-03-17 14:02:58