{"id":1713,"date":"2021-04-23T10:28:36","date_gmt":"2021-04-23T02:28:36","guid":{"rendered":"http:\/\/euhat.com\/wp\/?p=1713"},"modified":"2021-04-23T10:28:36","modified_gmt":"2021-04-23T02:28:36","slug":"%e8%ae%be%e7%bd%ae%e7%ba%bf%e7%a8%8b%e6%88%96%e8%bf%9b%e7%a8%8b%e8%a2%ab%e6%9d%80%e6%8e%89%e6%97%b6%e5%9b%9e%e8%b0%83%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/euhat.com\/wp\/2021\/04\/23\/%e8%ae%be%e7%bd%ae%e7%ba%bf%e7%a8%8b%e6%88%96%e8%bf%9b%e7%a8%8b%e8%a2%ab%e6%9d%80%e6%8e%89%e6%97%b6%e5%9b%9e%e8%b0%83%e7%9a%84%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u8bbe\u7f6e\u7ebf\u7a0b\u4e2d\u6b62\u65f6\u7684\u56de\u8c03\u51fd\u6570"},"content":{"rendered":"<p>pthread\u7248<\/p>\n<pre lang=\"cpp\" line=\"1\">\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <pthread.h>\n#include <signal.h>\n#include <unistd.h>\n\n#define WhThreadHandle pthread_t\n#define WH_THREAD_DEF(_proc, _arg) void *_proc(void *_arg)\n#define whThreadCreate(_handle, _loop, _param) pthread_create(&_handle, NULL, _loop, _param)\n\nvoid cleanup(void *p)\n{\n\tprintf(\"thread killed.\\n\");\n}\n\nWH_THREAD_DEF(func1, arg)\n{\n\tint oldState;\n\tpthread_cleanup_push(cleanup, NULL);\n\n\tpthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState);\n\twhile (1)\n\t\tsleep(1); \/\/ here must call sleep, or else cleanup will not be called.\n\tpthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);\n\n\tpthread_cleanup_pop(0); \/\/ here if want cleanup called, set param to non-zero.\n\treturn NULL;\n}\n\nWhThreadHandle threadHandle;\n\nvoid onSignal(int sign)\n{\n    switch (sign)\n    {\n    case SIGINT:\n\tpthread_cancel(threadHandle);\n        printf(\"process ctrl+c pressed.\\n\");\n        break;\n    }\n}\n\nint main()\n{\n\tsignal(SIGINT, onSignal);\n\n\twhThreadCreate(threadHandle, func1, 0);\n\n\twhile (1)\n\t\t;\n\treturn 1;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>pthread\u7248 #include #include #include #include #include #include #define WhThreadHandle pthread_t #define WH_THREAD_DEF(_proc, _arg) void *_proc(void *_arg) #define whThreadCreate(_handle, _loop, _param) pthread_create(&#038;_handle, NULL, _loop, _param) void cleanup(void *p) { printf(\"thread killed.\\n\"); } WH_THREAD_DEF(func1, arg) { int oldState; pthread_cleanup_push(cleanup, NULL); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &#038;oldState); while (1) sleep(1); \/\/ here must call sleep, or else cleanup will not be called. ... <a title=\"\u8bbe\u7f6e\u7ebf\u7a0b\u4e2d\u6b62\u65f6\u7684\u56de\u8c03\u51fd\u6570\" class=\"read-more\" href=\"http:\/\/euhat.com\/wp\/2021\/04\/23\/%e8%ae%be%e7%bd%ae%e7%ba%bf%e7%a8%8b%e6%88%96%e8%bf%9b%e7%a8%8b%e8%a2%ab%e6%9d%80%e6%8e%89%e6%97%b6%e5%9b%9e%e8%b0%83%e7%9a%84%e5%87%bd%e6%95%b0\/\" aria-label=\"More on \u8bbe\u7f6e\u7ebf\u7a0b\u4e2d\u6b62\u65f6\u7684\u56de\u8c03\u51fd\u6570\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[327,373,483,518],"_links":{"self":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts\/1713"}],"collection":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/comments?post=1713"}],"version-history":[{"count":0,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"wp:attachment":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}