\u201c\u5b89\u5168\u904d\u5386\u94fe\u8868\u201d<\/strong><\/span><\/div>\n\u4e3a\u4e86\u89e3\u51b3\u5728\u904d\u5386\u94fe\u8868\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u6cd5\u5220\u9664\u7ed3\u70b9\u7684\u95ee\u9898\uff0c\u5728 list.h \u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b89\u5168\u5220\u9664\u8282\u70b9\u7684\u51fd\u6570<\/p>\n
\/\/ \u5220\u9664\u91cd\u91cf\u5c0f\u4e8e300\u7684\u8282\u70b9\r\nvoid remove_unqualified_produce(void)\r\n{\r\nproduct_t *product, *temp;\r\nlist_for_each_entry_safe(product, temp, &product_list, product_node)\r\n{\r\n\/\/ \u79fb\u9664\u91cd\u91cf\u5c0f\u4e8e300\u7684\u4ea7\u54c1\r\nif (product->weight < 300)\r\n{\r\nprintf(\"remove product :%p, weight %d\\n\", product, product->weight);\r\nlist_del(&product->product_node);\r\nfree(product);\r\n}\r\n}\r\n}<\/pre>\n\u5176\u5b9e\u73b0\u662f\u4f7f\u7528\u4e00\u4e2a\u4e2d\u95f4\u53d8\u91cf\uff0c\u5728\u5f00\u59cb\u6bcf\u6b21\u5f00\u59cb\u6267\u884c\u5faa\u73af\u4f53\u524d\uff0c\u5c06\u5f53\u524d\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u4fdd\u5b58\u5230\u4e2d\u95f4\u53d8\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\"\u5b89\u5168\"\u904d\u5386<\/p>\n
#define list_for_each_entry_safe(pos, n, head, member) \\\r\nfor (pos = list_first_entry(head, typeof(*pos), member), \\\r\nn = list_next_entry(pos, member); \\\r\n&pos->member != (head); \\\r\npos = n, n = list_next_entry(n, member))<\/pre>\n\u591a\u7ebf\u7a0b\u4e2d\u4f7f\u7528list.h<\/strong><\/span><\/div>\n\u4e0a\u9762\u6211\u4eec\u5728\u4e3b\u7ebf\u7a0b\u91cc\u9762\u521b\u5efa\u4e86\u4ea7\u54c1\uff0c\u5e76\u653e\u5165\u5230\u94fe\u8868\u4e2d\u5e76\uff0c\u5e76\u8fc7\u6ee4\u4e86\u91cd\u91cf\u5c0f\u4e8e300\u7684\u4ea7\u54c1\u3002<\/p>\n
\u540e\u9762\u6211\u4eec\u5728\u591a\u7ebf\u7a0b\u4e2d\u5bf9\u4ea7\u54c1\u8fdb\u884c\u6d88\u8d39(\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6d88\u8d39\u94fe\u8868\u7684\u6570\u636e\uff0c\u4f7f\u7528\u5b8c\u6210\u540e\u5220\u9664\u5e76\u91ca\u653e\u7ed3\u70b9)\u3002<\/p>\n
\u8fd9\u91cc\u7684\u903b\u8f91\u548c\u5355\u7ebf\u7a0b\u4e2d\u7684\u5dee\u4e0d\u591a\uff0c\u540c\u6837\u662f\u904d\u5386\u94fe\u8868\uff0c\u7136\u540e\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u3002\u4e0d\u540c\u7684\u662f\uff0c\u7531\u4e8elist.h\u81ea\u8eab\u6ca1\u6709\u5e26\u9501\uff0c\u6240\u4ee5\u9700\u8981\u4f7f\u7528\u4e92\u65a5\u9501\u5c06\u94fe\u8868\u7684\u64cd\u4f5c\u8fdb\u884c\u4fdd\u62a4\u3002<\/p>\n
\u4e8e\u662f\u5f88\u81ea\u7136\u7684\u6709\u4e86\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n
void * consumer(void *arg)\r\n{\r\nproduct_t *product, *temp;\r\n\r\n\/\/ \u4f7f\u7528\u4e92\u65a5\u9501\u5bf9\u94fe\u8868\u8fdb\u884c\u4fdd\u62a4\r\npthread_mutex_lock(&producer_mutex);\r\nlist_for_each_entry_safe(product, temp, &product_list, product_node)\r\n{\r\nlist_del(&product->product_node);\r\nprintf(\"consume product :%p, weight %d, consumer :%p\\n\", product, product->weight, (void *)pthread_self());\r\npthread_mutex_unlock(&producer_mutex);\r\n\r\n\/\/ \u7761\u4e00\u4f1a\uff0c\u9632\u6b62\u592a\u5feb\u4e86\r\nusleep(10*1000);\r\nfree(product);\r\npthread_mutex_lock(&producer_mutex);\r\n}\r\npthread_mutex_unlock(&producer_mutex);\r\n\r\nreturn NULL;\r\n}<\/pre>\n\u5728\u4e0a\u9762\u7684\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5728\u5bf9\u94fe\u8868\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u4e92\u65a5\u9501\u5bf9\u94fe\u8868\u8fdb\u884c\u4e86\u4fdd\u62a4\uff0c\u4f7f\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u94fe\u8868\u3002<\/p>\n
\u4e0d\u8fc7\u4f60\u4ee5\u4e3a\u8fd9\u6837\u5c31\u597d\u4e86\u561b\uff0c\u5982\u679c\u65f6\u8fd9\u6837\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c31\u6ca1\u5b58\u5728\u7684\u5fc5\u8981\u4e86\u3002\u3002\u3002<\/p>\n
\u5728\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u904d\u5386\u65f6\uff0c\u5373\u4fbf\u662f\u52a0\u4e86\u9501\u4e4b\u540e\uff0c\u6570\u636e\u8bbf\u95ee\u4e5f\u4e0d\u5b89\u5168\u3002<\/p>\n
\u5728\u904d\u5386\u4f7f\u7528\u7684 list_for_each_entry_safe \u5b8f\u4e2d\uff0c\u4f7f\u7528\u4e86\u4e00\u4e2a\u96f6\u65f6\u53d8\u91cf\u5bf9\u4fdd\u5b58\u7740\u5f53\u524d\u94fe\u8868\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002<\/p>\n
\u4f46\u662f\u591a\u7ebf\u7a0b\u8bbf\u95ee\u94fe\u8868\u65f6\uff0c\u6709\u53ef\u80fd\u96f6\u65f6\u53d8\u91cf\u4fdd\u5b58\u7684\u8282\u70b9\uff0c\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5220\u9664\u4e86\uff0c\u6240\u4ee5\u8bbf\u95ee\u7684\u65f6\u5019\u53c8\u662f Segmentation fault<\/p>\n
\u540e\u8bb0<\/strong><\/div>\n\u539f\u56e0\u627e\u5230\u4e86\uff0c\u4e5f\u5c31\u597d\u529e\u4e86\u3002\u4ee5\u81f3\u4e8e\u89e3\u51b3\u65b9\u6cd5\u561b\uff0c\u6211\u662f\u4f7f\u7528\u4e00\u4e2a\u5168\u5c40\u7684\u96f6\u65f6\u53d8\u91cf\uff0c\u5c06\u9700\u8981\u5220\u9664\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u4fdd\u5b58\u8d77\u6765\uff0c\u624b\u52a8\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\"\u5b89\u5168\u5220\u9664\"\u3002<\/p>\n
\/\/ \u6d88\u8d39\u8005\r\nvoid * consumer(void *arg)\r\n{\r\nproduct_t *product;\r\n\r\n\/\/ \u4f7f\u7528\u4e92\u65a5\u9501\u5bf9\u94fe\u8868\u8fdb\u884c\u4fdd\u62a4\r\npthread_mutex_lock(&producer_mutex);\r\nlist_for_each_entry(product, &product_list, product_node)\r\n{\r\ntemp = list_next_entry(product, product_node);\r\nlist_del(&product->product_node);\r\nprintf(\"consume product :%p, weight %d, consumer :%p\\n\", product, product->weight, (void *)pthread_self());\r\npthread_mutex_unlock(&producer_mutex);\r\n\r\n\/\/ \u7761\u4e00\u4f1a\uff0c\u9632\u6b62\u592a\u5feb\r\nusleep(10*1000);\r\nfree(product);\r\n\r\npthread_mutex_lock(&producer_mutex);\r\nif(temp != NULL){\r\nproduct = list_prev_entry(temp, product_node);\r\n}\r\n}\r\npthread_mutex_unlock(&producer_mutex);\r\n\r\nreturn NULL;\r\n}<\/pre>\n\u4e00\u4e2a\u665a\u4e0a\u627e\u5230\u4e86\u8fd9\u4e2abug\uff0c\u7136\u540e\u53c8\u82b1\u4e86\u4e00\u4e2a\u665a\u4e0a\u8bb0\u5f55\u4e0b\u6765\u8fd9\u4e2abug\u3002<\/p>\n
\u636e\u8bf4 klist.h \u662f list.h \u7684\u7ebf\u7a0b\u5b89\u5168\u7248\u672c\uff0c\u540e\u9762\u82b1\u65f6\u95f4\u5728\u7814\u7a76\u4e00\u4e0b\u53bb\uff0c\u4eca\u5929\u5c31\u5148\u7761\u4e86\u3002\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
linux \u5f00\u53d1\u5e94\u8be5\u591a\u5c11\u90fd\u542c\u8fc7\u5927\u540d\u9f0e\u9f0e\u7684 list.h \uff0c\u5176\u7b80\u6d01\u4f18\u96c5\u7684\u8bbe\u8ba1\uff0c\u4e00\u4e2a\u5934\u6587\u4ef6\u5b8c\u6210\u4e86\u4e00\u4e2a\u9ad8\u53ef\u7528\u7684\u94fe\u8868\u3002 […]<\/p>\n","protected":false},"author":643,"featured_media":232323,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-232317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/232317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=232317"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/232317\/revisions"}],"predecessor-version":[{"id":232324,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/232317\/revisions\/232324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/232323"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=232317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=232317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=232317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}