{"id":342655,"date":"2021-02-12T20:14:45","date_gmt":"2021-02-12T12:14:45","guid":{"rendered":"http:\/\/4563.org\/?p=342655"},"modified":"2021-02-12T20:14:45","modified_gmt":"2021-02-12T12:14:45","slug":"c-%e8%af%ad%e8%a8%80%e5%ae%9e%e7%94%a8%e7%ac%ac%e4%b8%89%e6%96%b9%e5%ba%93-melon-%e4%b9%8b%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=342655","title":{"rendered":"C \u8bed\u8a00\u5b9e\u7528\u7b2c\u4e09\u65b9\u5e93 Melon \u4e4b\u591a\u7ebf\u7a0b\u5f00\u53d1"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C \u8bed\u8a00\u5b9e\u7528\u7b2c\u4e09\u65b9\u5e93 Melon \u4e4b\u591a\u7ebf\u7a0b\u5f00\u53d1               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<blockquote>\n<p>\u672c\u6587\u8f6c\u8f7d\u81ea\u672c\u4eba\u5934\u6761\u53f7\uff1a https:\/\/www.toutiao.com\/i6928959491274703371\/<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff0c\u611f\u8c22\uff01<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210219_602fcfcbce7b7.png\" alt=\"C \u8bed\u8a00\u5b9e\u7528\u7b2c\u4e09\u65b9\u5e93 Melon \u4e4b\u591a\u7ebf\u7a0b\u5f00\u53d1\" \/><\/p>\n<p>\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\uff08\u5f00\u53d1\u5229\u5668\u2014\u2014C \u8bed\u8a00\u5fc5\u5907\u5b9e\u7528\u7b2c\u4e09\u65b9\u5e93\uff09\uff0c\u7b14\u8005\u4ecb\u7ecd\u4e86\u4e00\u6b3e Linux\/UNIX \u4e0b C \u8bed\u8a00\u5e93 Melon \u7684\u57fa\u672c\u529f\u80fd\uff0c\u5e76\u7ed9\u51fa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u8fdb\u7a0b\u5f00\u7bb1\u5373\u7528\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u672c\u6587\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd Melon \u4e2d\u591a\u7ebf\u7a0b\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u63d0\u5230\u8fc7\uff0c\u5728 Melon \u4e2d\u6709\u4e24\u79cd\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff1a<\/p>\n<ul>\n<li>\u6a21\u5757\u5316\u7684\u591a\u7ebf\u7a0b\u6a21\u5f0f<\/li>\n<li>\u7ebf\u7a0b\u6c60<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5c06\u9010\u4e00\u7ed9\u51fa\u5b9e\u4f8b\u3002<\/p>\n<p>Melon \u7684 Github \u4ed3\u5e93\uff1a https:\/\/github.com\/Water-Melon\/Melon \u3002<\/p>\n<h2>\u6a21\u5757\u5316\u7ebf\u7a0b<\/h2>\n<p>\u6a21\u5757\u5316\u7ebf\u7a0b\u662f\u6307\uff0c\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u4ee3\u7801\u6a21\u5757\uff0c\u90fd\u6709\u5404\u81ea\u5bf9\u5e94\u7684\u5165\u53e3\u51fd\u6570\uff08\u7c7b\u4f3c\u4e8e\u6bcf\u4e00\u4e2a C \u8bed\u8a00\u7a0b\u5e8f\u6709\u4e00\u4e2a main \u51fd\u6570\u4e00\u6837\uff09\u3002<\/p>\n<p>\u6a21\u5757\u8981\u5b58\u653e\u4e8e Melon\/threads\/\u76ee\u5f55\u4e0b\u3002\u5728\u73b0\u6709\u7684 Melon \u4ee3\u7801\u4e2d\uff0c\u5305\u542b\u4e86\u4e24\u4e2a\u793a\u4f8b\u6a21\u5757\u2014\u2014haha \u548c hello \uff08\u540d\u5b57\u8d77\u5f97\u6709\u70b9\u968f\u610f\uff09\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u4ee5\u8fd9\u4e24\u4e2a\u6a21\u5757\u4e3a\u4f8b\u8bf4\u660e\u6a21\u5757\u5316\u7ebf\u7a0b\u7684\u5f00\u53d1\u548c\u4f7f\u7528\u6d41\u7a0b\u3002<\/p>\n<h3><strong>\u5f00\u53d1\u6d41\u7a0b<\/strong><\/h3>\n<p>\u8fd9\u91cc\u6709\u51e0\u70b9\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ol>\n<li>\u6a21\u5757\u7684\u540d\u5b57\uff1a\u6a21\u5757\u7684\u540d\u5b57\u5c06\u88ab\u7528\u4e8e\u4e24\u4e2a\u5730\u65b9\uff0c\u4e00\u4e2a\u662f\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u662f\u6a21\u5757\u5165\u53e3\u51fd\u6570\u540d\u3002\u524d\u8005\u5c06\u5728\u4f7f\u7528\u6d41\u7a0b\u4e2d\u8bf4\u660e\uff0c\u540e\u8005\u6211\u4eec\u9a6c\u4e0a\u5c06\u4ee5 haha \u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/li>\n<li>\u6a21\u5757\u7684\u53c2\u6570\uff1a\u53c2\u6570\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u7ed9\u51fa\u7684\uff0c\u8fd9\u4e00\u70b9\u6211\u4eec\u5728\u4f7f\u7528\u6d41\u7a0b\u4e2d\u5c06\u4f1a\u8bf4\u660e\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u4e00\u70b9\uff0c\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u5e76\u4e0d\u662f\u914d\u7f6e\u6587\u4ef6\u4e2d\u7ed9\u51fa\u7684\uff0c\u800c\u662f\u6846\u67b6\u81ea\u52a8\u8ffd\u52a0\u7684\uff0c\u662f\u4e3b\u7ebf\u7a0b\u4e0e\u8be5\u7ebf\u7a0b\u6a21\u5757\u901a\u4fe1\u7684 socketpair \u5957\u63a5\u5b57\u3002<\/li>\n<\/ol>\n<pre><code>\/\/haha \u6a21\u5757  int haha_main(int argc, char **argv) {      int fd = atoi(argv[argc-1]);     mln_thread_msg_t msg;     int nfds;     fd_set rdset;     for (;;) {         FD_ZERO(&amp;rdset);         FD_SET(fd, &amp;rdset);         nfds = select(fd+1, &amp;rdset, NULL, NULL, NULL);         if (nfds &lt; 0) {             if (errno == EINTR) continue;             mln_log(error, \"select error. %sn\", strerror(errno));             return -1;         }         memset(&amp;msg, 0, sizeof(msg));         int n = read(fd, &amp;msg, sizeof(msg));         if (n != sizeof(msg)) {             mln_log(debug, \"write error. n=%d. %sn\", n, strerror(errno));             return -1;         }         mln_log(debug, \"!!!src:%S auto:%l char:%cn\", msg.src, msg.sauto, msg.c);         mln_thread_clearMsg(&amp;msg);     }     return 0; } <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6a21\u5757\u7684\u5165\u53e3\u51fd\u6570\u540d\u4e3a haha_main \u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u6a21\u5757\u6765\u8bf4\uff0c\u4ed6\u4eec\u7684\u5165\u53e3\u51fd\u6570\u5c31\u662f\u4ed6\u4eec<strong>\u6a21\u5757\u7684\u540d\u79f0\uff08\u5373\u6587\u4ef6\u540d\uff09+\u4e0b\u5212\u7ebf+main<\/strong>\u7ec4\u6210\u7684\u3002<\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e5f\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5229\u7528 select \u6301\u7eed\u5173\u6ce8\u4e3b\u7ebf\u7a0b\u6d88\u606f\uff0c\u5f53\u4ece\u4e3b\u7ebf\u7a0b\u63a5\u6536\u5230\u6d88\u606f\u540e\uff0c\u5c31\u8fdb\u884c\u65e5\u5fd7\u8f93\u51fa\uff0c\u7136\u540e\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p>\u4e0e\u4e4b\u529f\u80fd\u5bf9\u5e94\u7684\u5c31\u662f hello \u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<pre><code>\/\/hello \u6a21\u5757 #include &lt;assert.h&gt;    static void hello_cleanup(void *data) {     mln_log(debug, \"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@n\"); }  int hello_main(int argc, char **argv) {     mln_thread_setCleanup(hello_cleanup, NULL);     int i;     for (i = 0; i &lt; 1; ++i)  {         int fd = atoi(argv[argc-1]);         mln_thread_msg_t msg;         memset(&amp;msg, 0, sizeof(msg));         msg.dest = mln_string_new(\"haha\");         assert(msg.dest);         msg.sauto = 9736;         msg.c = 'N';         msg.type = ITC_REQUEST;         msg.need_clear = 1;         int n = write(fd, &amp;msg, sizeof(msg));         if (n != sizeof(msg)) {             mln_log(debug, \"write error. n=%d. %sn\", n, strerror(errno));             mln_string_free(msg.dest);             return -1;         }     }     usleep(100000);     return 0; } <\/code><\/pre>\n<p>\u8fd9\u4e2a\u6a21\u5757\u7684\u529f\u80fd\u4e5f\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5411\u4e3b\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f\uff0c\u800c\u6d88\u606f\u7684\u63a5\u6536\u65b9\u662f haha \u6a21\u5757\uff0c\u5373\u4e3b\u7ebf\u7a0b\u662f\u4e00\u4e2a\u4e2d\u8f6c\u7ad9\uff0c\u5b83\u5c06 hello \u6a21\u5757\u7684\u6d88\u606f\u8f6c\u53d1\u7ed9 haha \u6a21\u5757\u3002<\/p>\n<p>\u5728 hello \u8fd9\u4e2a\u6a21\u5757\u4e2d\uff0c\u8c03\u7528\u4e86 mln_thread_setCleanup \u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\uff1a\u5728\u4ece\u5f53\u524d\u7ebf\u7a0b\u6a21\u5757\u7684\u5165\u53e3\u51fd\u6570\u8fd4\u56de\u81f3\u4e0a\u5c42\u51fd\u6570\u540e\uff0c\u5c06\u4f1a\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u6e05\u7406\u81ea\u5b9a\u4e49\u8d44\u6e90\u3002<\/p>\n<p>\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u6a21\u5757\u7684\u6e05\u7406\u51fd\u6570\u53ea\u80fd\u88ab\u8bbe\u7f6e\u4e00\u4e2a\uff0c\u591a\u6b21\u8bbe\u7f6e\u4f1a\u88ab\u8986\u76d6\uff0c\u6e05\u7406\u51fd\u6570\u662f\u7ebf\u7a0b\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u51fa\u73b0\u8986\u76d6\u5176\u4ed6\u7ebf\u7a0b\u5904\u7406\u51fd\u6570\u7684\u60c5\u51b5\uff08\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6545\u610f\u8fd9\u6837\u6765\u6784\u9020\uff0c\u6bd4\u5982\u4f20\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u6307\u9488\u7ed9\u522b\u7684\u6a21\u5757\uff0c\u7136\u540e\u90a3\u4e2a\u6a21\u5757\u518d\u8fdb\u884c\u8bbe\u7f6e\uff09\u3002<\/p>\n<h3><strong>\u4f7f\u7528\u6d41\u7a0b<\/strong><\/h3>\n<p>\u4f7f\u7528\u6d41\u7a0b\u9075\u5faa\u5982\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u7f16\u5199\u6846\u67b6\u542f\u52a8\u5668<\/li>\n<li>\u7f16\u8bd1\u94fe\u63a5\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f<\/li>\n<li>\u4fee\u6539\u914d\u7f6e\u6587\u4ef6<\/li>\n<li>\u542f\u52a8\u7a0b\u5e8f<\/li>\n<\/ol>\n<p>\u6211\u4eec\u9010\u4e2a\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<p>\u5173\u4e8e\u5982\u4f55\u5b89\u88c5\u5e93\uff0c\u53ef\u4ee5\u53c2\u8003 Github \u4ed3\u5e93\u8bf4\u660e\u6216\u8005\u4e4b\u524d\u7684\u6587\u7ae0\u3002<\/p>\n<p>\u6211\u4eec\u5148\u7f16\u5199\u542f\u52a8\u5668\uff1a<\/p>\n<pre><code>\/\/launcher.c  #include \"mln_core.h\"  int main(int argc, char *argv[]) {     struct mln_core_attr cattr;     cattr.argc = argc;     cattr.argv = argv;     cattr.global_init = NULL;     cattr.worker_process = NULL;     return mln_core_init(&amp;cattr); } <\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4e0d\u521d\u59cb\u5316\u4efb\u4f55\u5168\u5c40\u53d8\u91cf\uff0c\u4e5f\u4e0d\u9700\u8981\u5de5\u4f5c\u8fdb\u7a0b\uff0c\u56e0\u6b64\u90fd\u7f6e\u7a7a\u5373\u53ef\u3002<\/p>\n<pre><code>$ cc -o launcher launcher.c -I \/usr\/local\/melon\/include\/ -L \/usr\/local\/melon\/lib\/ -lmelon -lpthread <\/code><\/pre>\n<p>\u751f\u6210\u540d\u4e3a launcher \u7684\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u7684\u7ebf\u7a0b\u5c1a\u4e0d\u80fd\u6267\u884c\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>log_level \"none\"; \/\/user \"root\"; daemon off; core_file_size \"unlimited\"; \/\/max_nofile 1024; worker_proc 1; thread_mode off; framework off; log_path \"\/usr\/local\/melon\/logs\/melon.log\"; \/*  * Configurations in the 'exec_proc' are the  * processes which are customized by user.  *  * Here is an example to show you how to  * spawn a program.  *     keepalive \"\/tmp\/a.out\" [\"arg1\" \"arg2\" ...]  * The command in this example is 'keepalive' that  * indicate master process to supervise this  * process. If process is killed, master process  * would restart this program.  * If you don't want master to restart it, you can  *     default \"\/tmp\/a.out\" [\"arg1\" \"arg2\" ...]  *  * But you should know that there is another  * arugment after the last argument you write here.  * That is the file descriptor which is used to  * communicate with master process.  *\/ exec_proc {    \/\/ keepalive \"\/tmp\/a\"; } thread_exec { \/\/    restart \"hello\" \"hello\" \"world\"; \/\/    default \"haha\"; } <\/code><\/pre>\n<p>\u4e0a\u9762\u662f\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\uff0c\u6211\u4eec\u8981\u8fdb\u884c\u5982\u4e0b\u4fee\u6539\uff1a<\/p>\n<ul>\n<li>thread_mode off; -&gt; thread_mode on;<\/li>\n<li>framework off; -&gt; framework on;<\/li>\n<li>thread_exec \u914d\u7f6e\u5757\u4e2d\u7684\u4e24\u9879\u6ce8\u91ca\u53bb\u6389<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\uff0c\u9700\u8981\u989d\u5916\u8bf4\u660e\u4e00\u4e0b\uff1a<\/p>\n<p>thread_exec \u914d\u7f6e\u5757\u4e13\u95e8\u7528\u4e8e\u6a21\u5757\u5316\u7ebf\u7a0b\u4e4b\u7528\uff0c\u5176\u5185\u90e8\u6bcf\u4e00\u4e2a\u914d\u7f6e\u9879\u5747\u4e3a\u7ebf\u7a0b\u6a21\u5757\u3002<\/p>\n<p>\u4ee5 hello \u4e3a\u4f8b\uff1a<\/p>\n<pre><code>restart \"hello\" \"hello\" \"world\"; <\/code><\/pre>\n<p>restart \u6216\u8005 default \u662f\u6307\u4ee4\uff0crestart \u8868\u793a\u7ebf\u7a0b\u9000\u51fa\u4e3b\u51fd\u6570\u540e\uff0c\u518d\u6b21\u542f\u52a8\u7ebf\u7a0b\u3002\u800c default \u5219\u8868\u793a\u4e00\u65e6\u9000\u51fa\u4fbf\u4e0d\u518d\u542f\u52a8\u3002\u5176\u540e\u7684 hello \u5b57\u7b26\u4e32\u5c31\u662f\u6a21\u5757\u7684\u540d\u79f0\uff0c\u5176\u4f59\u5219\u4e3a\u6a21\u5757\u53c2\u6570\uff0c\u5373\u5165\u53e3\u51fd\u6570\u7684 argc \u548c argv \u7684\u90e8\u5206\u3002\u800c\u4e0e\u4e3b\u7ebf\u7a0b\u901a\u4fe1\u7684\u5957\u63a5\u5b57\u5219\u4e0d\u5fc5\u5199\u5728\u6b64\u5904\uff0c\u800c\u662f\u7ebf\u7a0b\u542f\u52a8\u540e\u8fdb\u5165\u5165\u53e3\u51fd\u6570\u524d\u81ea\u52a8\u6dfb\u52a0\u7684\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5c31\u6765\u542f\u52a8\u7a0b\u5e8f\u5427\u3002<\/p>\n<pre><code>$ .\/launcher  Start up worker process No.1 Start thread 'hello' Start thread 'haha' 02\/14\/2021 04:07:48 GMT DEBUG: .\/src\/mln_thread_module.c:haha_main:42: PID:9309 !!!src:hello auto:9736 char:N 02\/14\/2021 04:07:49 GMT DEBUG: .\/src\/mln_thread_module.c:hello_cleanup:53: PID:9309 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 Thread 'hello' return 0. 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 Child thread 'hello' exit. 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 child thread pthread_join's exit code: 0 02\/14\/2021 04:07:49 GMT DEBUG: .\/src\/mln_thread_module.c:haha_main:42: PID:9309 !!!src:hello auto:9736 char:N 02\/14\/2021 04:07:49 GMT DEBUG: .\/src\/mln_thread_module.c:hello_cleanup:53: PID:9309 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 Thread 'hello' return 0. 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 Child thread 'hello' exit. 02\/14\/2021 04:07:49 GMT REPORT: PID:9309 child thread pthread_join's exit code: 0 ... <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e8b\u5b9e\u4e0a Melon \u4e2d\u4f1a\u542f\u52a8\u5de5\u4f5c\u8fdb\u7a0b\u6765\u62c9\u8d77\u5176\u5b50\u7ebf\u7a0b\uff0c\u800c\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\u7531 worker_proc \u914d\u7f6e\u9879\u63a7\u5236\uff0c\u5982\u679c\u591a\u4e8e\u4e00\u4e2a\uff0c\u5219\u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u90fd\u4f1a\u62c9\u8d77\u4e00\u7ec4 haha \u548c hello \u7ebf\u7a0b\u3002\u6b64\u5916\uff0c\u6211\u4eec\u4e5f\u770b\u5230\uff0chello \u7ebf\u7a0b\u9000\u51fa\u540e\uff0c\u6e05\u7406\u51fd\u6570\u88ab\u8c03\u7528\u3002<\/p>\n<h2>\u7ebf\u7a0b\u6c60<\/h2>\n<p>\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\u5219\u4e0e\u6846\u67b6\u57fa\u672c\u65e0\u5173\uff0c\u5168\u90e8\u662f\u5bf9\u5c01\u88c5\u597d\u7684\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u914d\u7f6e\u6587\u4ef6\u6062\u590d\u4e3a\u521a\u5b89\u88c5\u597d\u65f6\u7684\u9ed8\u8ba4\u914d\u7f6e\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include \"mln_core.h\" #include \"mln_thread_pool.h\" #include \"mln_log.h\"  static int main_process_handler(void *data); static int child_process_handler(void *data); static void free_handler(void *data);  int main(int argc, char *argv[]) {     struct mln_core_attr cattr;     struct mln_thread_pool_attr tpattr;      cattr.argc = argc;     cattr.argv = argv;     cattr.global_init = NULL;     cattr.worker_process = NULL;     if (mln_core_init(&amp;cattr) &lt; 0) {         return -1;     }      tpattr.dataForMain = NULL;     tpattr.child_process_handler = child_process_handler;     tpattr.main_process_handler = main_process_handler;     tpattr.free_handler = free_handler;     tpattr.condTimeout = 10;     tpattr.max = 10;     tpattr.concurrency = 10;     return mln_thread_pool_run(&amp;tpattr); }  static int child_process_handler(void *data) {     mln_log(none, \"%sn\", (char *)data);     return 0; }  static int main_process_handler(void *data) {     int n;     char *text;      while (1) {         if ((text = (char *)malloc(16)) == NULL) {             return -1;         }         n = snprintf(text, 15, \"hello world\");         text[n] = 0;         mln_thread_pool_addResource(text);         usleep(1000);     } }  static void free_handler(void *data) {     free(data); } <\/code><\/pre>\n<p>\u4e3b\u51fd\u6570\u4e2d\u5148\u5bf9 Melon \u6846\u67b6\u505a\u4e86\u521d\u59cb\u5316\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u521d\u59cb\u5316\u65e5\u5fd7\uff0c\u56e0\u4e3a\u914d\u7f6e\u6587\u4ef6\u4e2d\u5c06\u4e0d\u542f\u7528\u6846\u67b6\u3002\u7136\u540e\u521d\u59cb\u5316\u7ebf\u7a0b\u6c60\u3002<\/p>\n<p>\u7a0b\u5e8f\u529f\u80fd\u6bd4\u8f83\u7b80\u5355\uff0c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u8d44\u6e90\u7136\u540e\u5206\u53d1\u8d44\u6e90\uff0c\u5b50\u7ebf\u7a0b\u62ff\u5230\u8d44\u6e90\u5e76\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<p>\u6240\u6709\u8d44\u6e90\u5206\u53d1\u4ee5\u53ca\u8d44\u6e90\u7ade\u4e89\u5168\u90e8\u5c01\u88c5\u5728\u51fd\u6570\u5185\u90e8\uff0c\u56de\u8c03\u51fd\u6570\u53ea\u9700\u8981\u505a\u529f\u80fd\u903b\u8f91\u5904\u7406\u5373\u53ef\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u88ab\u521d\u59cb\u5316\u4e3a\u6700\u5927\u6709 10 \u4e2a\u5b50\u7ebf\u7a0b\u540c\u65f6\u5904\u7406\uff0c\u82e5\u5f53\u524d\u67d0\u4e00\u5b50\u7ebf\u7a0b\u95f2\u7f6e\u65f6\u95f4\u8d85\u8fc7 10 \u79d2\uff0c\u5219\u4f1a\u88ab\u56de\u6536\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u5e76\u6267\u884c\uff1a<\/p>\n<pre><code>$ cc -o hello hello.c -I \/usr\/local\/melon\/include\/ -L \/usr\/local\/melon\/lib\/ -lmelon -lpthread $ .\/hello hello world hello world hello world hello world ... <\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u6267\u884c<\/p>\n<pre><code>$ top -d 1 -H -p PID <\/code><\/pre>\n<p>PID \u4e3a hello \u7a0b\u5e8f\u7684\u8fdb\u7a0b ID\uff0c\u5219\u4f1a\u770b\u5230\uff0c\u5076\u5c14\u4f1a\u51fa\u73b0\u4e24\u4e2a\u7ebf\u7a0b\uff08\u5982\u679c\u673a\u5668\u6027\u80fd\u8f83\u597d\u53ef\u80fd\u770b\u4e0d\u5230\uff0c\u90a3\u4e48\u5c31\u7f29\u77ed usleep \u7684\u65f6\u95f4\u5373\u53ef\uff09\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce\u5927\u5bb6\u7559\u8a00\u8bc4\u8bba\u3002<\/p>\n<p>\u518d\u6b21\u7ed9\u51fa Melon \u7684\u5b98\u65b9 QQ \u7fa4\uff1a756582294<\/p>\n<p>Github \u4ed3\u5e93\uff1a https:\/\/github.com\/Water-Melon\/Melon<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5279276\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5404\u4f4d\u5b9e\u5728\u4e0d\u597d\u610f\u601d\uff0c\u4e4b\u524d QQ \u7fa4\u8bbe\u7f6e\u6709\u95ee\u9898\u5bfc\u81f4\u65e0\u6cd5\u641c\u7d22\u5230\uff0c\u611f\u8c22\u53cd\u9988\uff0c\u73b0\u5728\u5df2\u7ecf\u53ef\u4ee5\u641c\u7d22\u5230\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00\u5b9e\u7528\u7b2c\u4e09\u65b9\u5e93 Melon &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/342655"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342655"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/342655\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342655"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}