{"id":1821,"date":"2019-01-13T11:11:42","date_gmt":"2019-01-13T03:11:42","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1821"},"modified":"2020-12-02T12:41:47","modified_gmt":"2020-12-02T04:41:47","slug":"linux%e7%bc%96%e7%a8%8b-%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2019\/01\/13\/1111421821.html","title":{"rendered":"Linux\u7f16\u7a0b&#8211;\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"<h3>1\u3001\u4ec0\u4e48\u662f\u540c\u6b65\uff1f<\/h3>\n<ul>\n<li>\u540c\uff0c\u6307\u534f\u540c\u3001\u534f\u52a9\u3001\u4e92\u76f8\u914d\u5408\u3002\u4e3b\u65e8\u5728\u534f\u540c\u6b65\u8c03\uff0c\u6309\u9884\u5b9a\u7684\u5148\u540e\u6b21\u5e8f\u8fd0\u884c\u3002<\/li>\n<li>\u7ebf\u7a0b\u540c\u6b65\uff0c\u6307\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u51fa\u67d0\u4e00\u529f\u80fd\u8c03\u7528\u65f6\uff0c\u5728\u6ca1\u6709\u5f97\u5230\u7ed3\u679c\u4e4b\u524d\uff0c\u8be5\u8c03\u7528\u4e0d\u8fd4\u56de\u3002\u540c\u65f6\u5176\u5b83\u7ebf\u7a0b\u4e3a\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4e0d\u80fd\u8c03\u7528\u8be5\u529f\u80fd\u3002<\/li>\n<li>\u540c\u6b65\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u6570\u636e\u6df7\u4e71\uff0c\u89e3\u51b3\u4e0e\u65f6\u95f4\u6709\u5173\u7684\u9519\u8bef\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0d\u4ec5\u7ebf\u7a0b\u95f4\u9700\u8981\u540c\u6b65\uff0c\u8fdb\u7a0b\u95f4\u3001\u4fe1\u53f7\u95f4\u7b49\u90fd\u9700\u8981\u540c\u6b65\u673a\u5236\u3002<\/li>\n<li>\u603b\u7ed3\uff1a\u6240\u6709\u201c\u591a\u4e2a\u63a7\u5236\u6d41\uff0c\u5171\u540c\u64cd\u4f5c\u4e00\u4e2a\u5171\u4eab\u8d44\u6e90\u201d\u7684\u60c5\u51b5\uff0c\u90fd\u9700\u8981\u540c\u6b65\u3002<!--more--><\/li>\n<\/ul>\n<h3>2\u3001\u6570\u636e\u6df7\u4e71\u7684\u539f\u56e0<\/h3>\n<ul>\n<li>\u8d44\u6e90\u5171\u4eab\uff08\u72ec\u4eab\u8d44\u6e90\u4e0d\u4f1a\uff09<\/li>\n<li>\u8c03\u5ea6\u968f\u673a\uff08\u610f\u5473\u7740\u6570\u636e\u8bbf\u95ee\u4f1a\u51fa\u73b0\u7ade\u4e89\uff09<\/li>\n<li>\u7ebf\u7a0b\u95f4\u7f3a\u4e4f\u5fc5\u8981\u7684\u540c\u6b65\u673a\u5236\u3002<br \/>\n\u4ee5\u4e0a\u4e09\u70b9\u4e2d\uff0c\u524d\u4e24\u70b9\u4e0d\u80fd\u6539\u53d8\uff0c\u6b32\u63d0\u9ad8\u6548\u7387\uff0c\u4f20\u9012\u6570\u636e\uff0c\u8d44\u6e90\u5fc5\u987b\u5171\u4eab\u3002\u53ea\u8981\u5171\u4eab\u8d44\u6e90\uff0c\u5c31\u4e00\u5b9a\u4f1a\u51fa\u73b0\u7ade\u4e89\u3002\u53ea\u8981\u5b58\u5728\u7ade\u4e89\u5173\u7cfb\uff0c\u6570\u636e\u5c31\u5f88\u5bb9\u6613\u51fa\u73b0\u6df7\u4e71\u3002<br \/>\n\u6240\u4ee5\u53ea\u80fd\u4ece\u7b2c\u4e09\u70b9\u7740\u624b\u89e3\u51b3\u3002\u4f7f\u591a\u4e2a\u7ebf\u7a0b\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u65f6\u5019\uff0c\u51fa\u73b0\u4e92\u65a5\u3002<\/li>\n<\/ul>\n<h3>3\u3001\u4e92\u65a5\u91cf\uff08\u4e92\u65a5\u9501\uff09<\/h3>\n<ul>\n<li>Linux\u4e2d\u63d0\u4f9b\u4e00\u628a\u4e92\u65a5\u9501mutex\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u5bf9\u8d44\u6e90\u64cd\u4f5c\u524d\u90fd\u5c1d\u8bd5\u5148\u52a0\u9501\uff0c\u6210\u529f\u52a0\u9501\u624d\u64cd\u4f5c\uff0c\u64cd\u4f5c\u7ed3\u675f\u89e3\u9501\u3002<\/li>\n<li>\u8d44\u6e90\u8fd8\u662f\u5171\u4eab\u7684\uff0c\u7ebf\u7a0b\u95f4\u4e5f\u8fd8\u662f\u7ade\u4e89\u7684\uff0c\u4f46\u901a\u8fc7\u201c\u9501\u201d\u5c06\u8d44\u6e90\u8bbf\u95ee\u53d8\u6210\u4e92\u65a5\u64cd\u4f5c\uff0c\u800c\u540e\u7684\u65f6\u95f4\u6709\u5173\u9519\u8bef\u4e5f\u4e0d\u4f1a\u518d\u4ea7\u751f\u4e86\u3002<\/li>\n<li>\u540c\u4e00\u65f6\u95f4\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u8be5\u9501\u3002<\/li>\n<li>\u4e3b\u8981\u51fd\u6570\uff1a\n<pre><code class=\"language-c line-numbers\">\/\/ restrict\u5173\u952e\u5b57\uff1a\u7528\u4e8e\u9650\u5236\u6307\u9488\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6240\u6709\u4fee\u6539\u8be5\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u4e2d\u5185\u5bb9\u7684\u64cd\u4f5c\uff0c\u53ea\u80fd\u901a\u8fc7\u672c\u6307\u9488\u5b8c\u6210\u3002\nint pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);\npthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\n\nint pthread_mutex_destroy(pthread_mutex_t *mutex);\n\nint pthread_mutex_lock(pthread_mutex_t *mutex);\n\nint pthread_mutex_trylock(pthread_mutex_t *mutex);\n\nint pthread_mutex_unlock(pthread_mutex_t *mutex);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n\/\/ 1\u3001\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4e92\u65a5\u9501\npthread_mutex_t mutex;\n\/\/ \u7ebf\u7a0b\u56de\u8c03\u51fd\u6570\nvoid *callback(void *agr){\n    while(1){\n        \/\/ \u52a0\u9501\u3001\u6b64\u65f6\u9700\u8981\u4f7f\u7528mutex\u7684\u7ebf\u7a0b\u5728\u8be5\u9501\u88ab\u91ca\u653e\u524d\u4e0d\u80fd\u4f7f\u7528\n        pthread_mutex_lock(&amp;mutex);\n        printf(\"123\");\n        sleep(1);\n        printf(\"456\\n\u201d);\n        \/\/ \u89e3\u9501\u3001\u91ca\u653e\u51fa\u8d44\u6e90\n        pthread_mutex_unlock(&amp;mutex);\n        sleep(1);\n    }\n    return NULL;\n}\n\nint main(){\n    \/\/ 2\u3001\u521d\u59cb\u5316\u4e92\u65a5\u9501\n    pthread_mutex_init(&amp;mutex, NULL);\n\n    pthread_t tid;\n    pthread_create(&amp;tid, NULL, callback, NULL);\n    \/\/ 3\u3001\u4f7f\u7528\u9501\n    while(1){\n        pthread_mutex_lock(&amp;mutex);\n        printf(\"abc\");\n        sleep(2);\n        printf(\"def\\n\");\n        pthread_mutex_unlock(&amp;mutex);\n        sleep(1);\n    }\n    \/\/ 4\u3001\u91ca\u653e\/\u9500\u6bc1\u4e92\u65a5\u9501\n    pthread_mutex_destroy(&amp;mutex);\n    return 0;\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-cmd line-numbers\">\/\/ \u4f7f\u7528\u4e92\u65a5\u9501\u524d\nparallels@ubuntu:~\/Linux\/pthread_syn$ .\/pthread_mutex.out\nabc123456\n123456\n123def\nabc456\n123def\nabc456\n123456\n^C\nparallels@ubuntu:~\/Linux\/pthread_syn$ \n\/\/ \u4f7f\u7528\u4e92\u65a5\u9501\u540e\nparallels@ubuntu:~\/Linux\/pthread_syn$ .\/pthread_mutex.out\nabcdef\n123456\nabcdef\n123456\nabcdef\n123456\nabcdef\n^C\nparallels@ubuntu:~\/Linux\/pthread_syn$\n<\/code><\/pre>\n<p><strong>PS\uff1a\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u524d\u52a0\u9501\uff0c\u8bbf\u95ee\u7ed3\u675f\u540e\u7acb\u5373\u89e3\u9501\u3002\u9501\u7684\u201c\u7c92\u5ea6\u201d\u5e94\u8d8a\u5c0f\u8d8a\u597d\u3002<\/strong><\/p>\n<h3>4\u3001\u6b7b\u9501<\/h3>\n<ul>\n<li>\u540c\u4e00\u7ebf\u7a0b\u91cd\u590d\u52a0\u9501\uff0c\u4f1a\u9020\u6210\u81ea\u5df1\u7b49\u81ea\u5df1\uff0c\u6b7b\u9501\uff1b<\/li>\n<li>\u4e24\u4e2a\u7ebf\u675f\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u7684\u9501\uff0c\u6b7b\u9501<\/li>\n<\/ul>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n\/\/ 1\u3001\u5b9a\u4e49\u4e24\u4e2a\u4e92\u65a5\u9501\npthread_mutex_t mutex1, mutex2;\n\/\/ \u5b50\u7ebf\u7a0b\u56de\u8c03\u51fd\u6570\nvoid *callback(void *arg){\n    \/\/ \u5bf9mutex1\u52a0\u9501\n    pthread_mutex_lock(&amp;mutex1);\n    printf(\"sub pthread: I get mutex1....\\n\");\n    sleep(1);\n    \/\/ \u5bf9mutex2\u52a0\u9501\n    pthread_mutex_lock(&amp;mutex2);\n    printf(\"sub pthread: I get mutex2...\\n\");\n    pthread_mutex_unlock(&amp;mutex2);\n    pthread_mutex_unlock(&amp;mutex1);\n    return NULL;\n}\n\nint main(){\n    \/\/ 2\u3001\u521d\u59cb\u5316\u4e24\u628a\u4e92\u65a5\u9501\n    pthread_mutex_init(&amp;mutex1, NULL);\n    pthread_mutex_init(&amp;mutex2, NULL);    \n\n    \/\/ 3\u3001\u521b\u5efa\u5b50\u7ebf\u7a0b\n    pthread_t tid;\n    pthread_create(&amp;tid, NULL, callback, NULL);\n\n    \/\/ 4\u3001\u4e3b\u7ebf\u7a0b\u4e0e\u5b50\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u7684\u9501\n    pthread_mutex_lock(&amp;mutex2);\n    printf(\"main pthread: I get mutex2...\\n\");\n    sleep(1);\n    pthread_mutex_lock(&amp;mutex1);\n    printf(\"main pthread: I get mutex1...\\n\");\n    pthread_mutex_unlock(&amp;mutex2);\n    pthread_mutex_unlock(&amp;mutex1);\n\n    \/\/ \u9500\u6bc1\u91ca\u653e\n    pthread_mutex_destroy(&amp;mutex1);\n    pthread_mutex_destroy(&amp;mutex2);\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-cmd line-numbers\">parallels@ubuntu:~\/Linux\/pthread_syn$ .\/pthread_mutex_deadlock.out\nmain pthread: I get mutex2...\nsub pthread: I get mutex1....\n^C\nparallels@ubuntu:~\/Linux\/pthread_syn$\n<\/code><\/pre>\n<h3>5\u3001\u8bfb\u5199\u9501<\/h3>\n<ul>\n<li>\u5199\u52a0\u9501\uff1a\u4e0d\u53ef\u8bfb\u5199\uff0c\u963b\u585e\u8bfb\u5199\u7ebf\u7a0b<\/li>\n<li>\u8bfb\u52a0\u9501\uff1a\u53ef\u8bfb\u4e0d\u53ef\u5199\uff0c\u963b\u585e\u5199\u7ebf\u7a0b<\/li>\n<li><strong>\u5199\u4f18\u5148\u7ea7\u9ad8\uff0c\u5199\u72ec\u5360\u3001\u8bfb\u5171\u4eab\u3002<\/strong>\n<pre><code class=\"language-c line-numbers\">int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);\n\nint pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);\n\nint pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);\n\nint pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);\n\nint pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);\n\nint pthread_rwlock_unlock(pthread_rwlock_t *rwlock);\n\nint pthread_rwlock_destroy(pthread_rwlock_t *rwlock);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;string.h&gt;\n\/\/ 1\u3001\u5b9a\u4e49\u8bfb\u5199\u9501\npthread_rwlock_t rwlock;\n\/\/ \u201c\u5199\u201d\u76f8\u5173\u7ebf\u7a0b\u4e3b\u51fd\u6570\nvoid *callback_read(void *arg){\n    while(1){\n        pthread_rwlock_rdlock(&amp;rwlock);\n        printf(\"I am a pthread -- read %d --, I get the rwlock now...\\n\", (int)(long)arg);\n        sleep(1);\n        pthread_rwlock_unlock(&amp;rwlock);\n        usleep(300);\n    }\n    return NULL;\n}\n\/\/ \u201c\u8bfb\u201d\u76f8\u5f53\u7ebf\u7a0b\u4e3b\u51fd\u6570\nvoid *callback_write(void *arg){\n    while(1){\n        pthread_rwlock_wrlock(&amp;rwlock);\n        printf(\"----------------------------------------------------\\n\");\n        printf(\"I am a pthread -- writ %d --, I get the rwlock now...\\n\", (int)(long)arg);\n        printf(\"----------------------------------------------------\\n\");\n        sleep(1);\n        pthread_rwlock_unlock(&amp;rwlock);\n        usleep(300);\n    }\n    return NULL;\n}\n\nint main(){\n    \/\/ 2\u3001\u521d\u59cb\u5316\u8bfb\u5199\u9501\n    pthread_rwlock_init(&amp;rwlock, NULL);\n    \/\/ 3\u3001\u521b\u5efa10\u4e2a\u7ebf\u7a0b\uff0c6\u4e2a\u8bfb4\u4e2a\u5199\n    pthread_t tid[10];\n    int i, ret;\n    for (i = 0; i &lt; 6; i++){\n        ret = pthread_create(&amp;tid[i], NULL, callback_read, (void*)(long)i);\n        if (ret != 0){\n            fprintf(stderr, \"pthread_create error: %s\\n\", strerror(ret));\n            continue;\n        }\n    }\n    for (i = 6; i &lt; 10; i++){\n        ret = pthread_create(&amp;tid[i], NULL, callback_write, (void *)(long)i);\n        if (ret != 0){\n            fprintf(stderr, \"pthread_create error: %s\\n\", strerror(ret));\n            continue;\n        }\n    }\n    \/\/ \u56de\u6536\u5b50\u7ebf\u7a0b\n    for (i = 0; i &lt; 10; i++){\n        ret = pthread_join(tid[i], NULL);\n        if (ret != 0){\n            fprintf(stderr, \"pthread_join error:%s\\n\", strerror(ret));\n            continue;\n        }\n    }\n    \/\/ 4\u3001\u56de\u6536\u91ca\u653e\u8bfb\u5199\u9501\n    pthread_rwlock_destroy(&amp;rwlock);\n    pthread_exit(NULL);\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-cmd line-numbers\">parallels@ubuntu:~\/Linux\/pthread_syn$ .\/pthread_rwlock.out\nI am a pthread -- read 0 --, I get the rwlock now...\nI am a pthread -- read 1 --, I get the rwlock now...\nI am a pthread -- read 2 --, I get the rwlock now...\nI am a pthread -- read 3 --, I get the rwlock now...\nI am a pthread -- read 4 --, I get the rwlock now...\nI am a pthread -- read 5 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- writ 6 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- read 1 --, I get the rwlock now...\nI am a pthread -- read 2 --, I get the rwlock now...\nI am a pthread -- read 0 --, I get the rwlock now...\nI am a pthread -- read 3 --, I get the rwlock now...\nI am a pthread -- read 5 --, I get the rwlock now...\nI am a pthread -- read 4 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- writ 7 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- read 1 --, I get the rwlock now...\nI am a pthread -- read 2 --, I get the rwlock now...\nI am a pthread -- read 0 --, I get the rwlock now...\nI am a pthread -- read 3 --, I get the rwlock now...\nI am a pthread -- read 5 --, I get the rwlock now...\nI am a pthread -- read 4 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- writ 9 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- read 1 --, I get the rwlock now...\nI am a pthread -- read 2 --, I get the rwlock now...\nI am a pthread -- read 4 --, I get the rwlock now...\nI am a pthread -- read 0 --, I get the rwlock now...\nI am a pthread -- read 3 --, I get the rwlock now...\nI am a pthread -- read 5 --, I get the rwlock now...\n----------------------------------------------------\nI am a pthread -- writ 8 --, I get the rwlock now...\n----------------------------------------------------\n^C\nparallels@ubuntu:~\/Linux\/pthread_syn$\n<\/code><\/pre>\n<h3>6\u3001\u6761\u4ef6\u53d8\u91cf<\/h3>\n<pre><code class=\"language-c line-numbers\">int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);\n\nint pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);\n\nint pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);\n\nint pthread_cond_signal(pthread_cond_t *cond);\n\nint pthread_cond_broadcast(pthread_cond_t *cond);\n\nint pthread_cond_destroy(pthread_cond_t *cond);\n<\/code><\/pre>\n<ul>\n<li>int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);\n<ol>\n<li>\u963b\u585e\u7b49\u5f85\u6761\u4ef6\u53d8\u91cfcond<\/li>\n<li>\u91ca\u653e\u5df2\u638c\u63e1\u7684\u4e92\u65a5\u9501\uff0c\u76f8\u5f53\u4e8epthread_mutex_unlock(&amp;mutex)<\/li>\n<li>\u5524\u9192\u65f6\u89e3\u51b3\u963b\u585e\uff0c\u91cd\u65b0\u7533\u8bf7\u4e92\u65a5\u9501pthread_mutex_lock(&amp;mutex)<\/li>\n<li>PS\uff1a\u524d\u4e24\u6b65\u4e3a\u539f\u5b50\u64cd\u4f5c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n\/\/ 1\u3001\u5b9a\u4e49\u4e92\u65a5\u9501\u3001\u6761\u4ef6\u53d8\u91cf\npthread_mutex_t mutex;\npthread_cond_t cond;\n\/\/ \u5b50\u7ebf\u7a0b\u4e3b\u51fd\u6570\nvoid *callback(void *arg){\n    pthread_mutex_lock(&amp;mutex);\n    int i = 10;\n    while(i--){\n        printf(\"I am sub pthread runloop at %d\\n\", i);\n        \/\/ \u4e2d\u95f4\u8bbe\u8ba1\n        if (i == 7){\n            pthread_cond_wait(&amp;cond, &amp;mutex);\n            \/* \u4f7f\u7528pthread_cond_timedwait\u51fd\u6570\n            struct timespec t;\n            t.tv_sec = time(NULL) + 3;\n            \/\/ \u7b2c\u4e09\u4e2a\u53c2\u6570const struct timespec *restrict abstime\n            \/\/ timespec{time_t tv_sec;time_t tv_usec}\n            pthread_cond_timedwait(&amp;cond, &amp;mutex, &amp;t);\n            *\/\n        }\n        \/\/sleep(1);\n    }\n    pthread_mutex_unlock(&amp;mutex);\n    return NULL;\n}\n\nint main(){\n    \/\/ 2\u3001\u521d\u59cb\u5316\u4e92\u65a5\u9501\u3001\u6761\u4ef6\u53d8\u91cf\n    pthread_mutex_init(&amp;mutex, NULL);\n    pthread_cond_init(&amp;cond, NULL);\n    \/\/ 3\u3001\u5f00\u542f\u5b50\u7ebf\u7a0b\n    pthread_t tid;\n    pthread_create(&amp;tid, NULL, callback, NULL);\n\n    \/\/ 4\u3001\u53d1\u9001\u6761\u4ef6\u53d8\u91cf\u4fe1\u53f7\n    sleep(3);\n    pthread_cond_signal(&amp;cond);\n\n    \/\/ 5\u3001\u6ce8\u9500\u6761\u4ef6\u53d8\u91cf\u3001\u4e92\u65a5\u9501\n    pthread_cond_destroy(&amp;cond);\n    pthread_mutex_destroy(&amp;mutex);\n    \/\/ \u56de\u6536\u5b50\u7ebf\u7a0b\uff0c\u9000\u51fa\u5f53\u524d\u7ebf\u7a0b\n    pthread_join(tid, NULL);\n    pthread_exit(NULL);\n}\n<\/code><\/pre>\n<h3>7\u3001\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b<\/h3>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n\n\/\/ 1.1\u3001\u5b9a\u4e49\u5e76\u521b\u5efa\u4e92\u65a5\u9501\u3001\u6761\u4ef6\u53d8\u91cf\npthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\npthread_cond_t cond = PTHREAD_COND_INITIALIZER;\n\n\/\/ 1.2\u3001\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u4e0e\u6570\u636e\u7ed3\u6784\n\/\/ \u6d4b\u8bd5\u7528\u7684\u6570\u636e\u6a21\u578b\nstruct msg{\n    struct msg *next;\n    int num;\n};\n\/\/ \u5168\u5c40\u94fe\u8868\nstruct msg *head;\n\/\/ \u6253\u5370\u8f93\u51fa\u201c\u672a\u6d88\u8d39\u201d\u7684\u4ea7\u54c1\nvoid print_msg(){\n    printf(\" | \");\n    struct msg *msg = head;\n    while(msg){\n        printf(\"%d \", msg-&gt;num);\n        msg = msg-&gt;next;\n    }\n    putchar('\\n');\n}\n\n\/\/ 3.1\u3001\u751f\u4ea7\u8005\u7ebf\u7a0b\u4e3b\u51fd\u6570\nvoid *producer(void *arg){\n    while(1){\n        pthread_mutex_lock(&amp;mutex);\n        \/\/ \u4ea7\u751f\u4e00\u6761\u65b0\u6570\u636e\n        struct msg *msg = malloc(sizeof(struct msg));\n        msg-&gt;num = rand() % 99;\n\n        \/\/ \u5728\u961f\u5934\u63d2\u5165\u6570\u7ec4\n        msg-&gt;next = head;\n        head = msg;\n\n        \/\/ \u6253\u5370\u5f53\u524d\u7ed3\u679c\n        printf(\"producer msg with num = %d\", msg-&gt;num);\n        print_msg();\n        pthread_mutex_unlock(&amp;mutex);\n\n        pthread_cond_signal(&amp;cond);\n        sleep(rand() % 3);\n    }\n}\n\/\/ 3.2\u3001\u6d88\u8d39\u8005\u7ebf\u7a0b\u4e3b\u51fd\u6570\nvoid *consumer(void *arg){\n    while (1){\n        pthread_mutex_lock(&amp;mutex);\n        if (head == NULL){\n            pthread_cond_wait(&amp;cond, &amp;mutex);\n        }\n        \/\/ \u4ece\u961f\u5934\u53d6\u6570\u636e\n        struct msg *msg = head;\n        head = msg-&gt;next;\n\n        \/\/ \u6253\u5370\u5f53\u524d\u7ed3\u679c\n        printf(\"consumer msg with num = %d\", msg-&gt;num);\n        print_msg();\n        pthread_mutex_unlock(&amp;mutex);\n\n        free(msg);\n        sleep(rand() % 3);\n    }\n}\n\nint main(){\n    \/\/ \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\n    srand(time(NULL));\n    \/\/ 2\u3001\u521b\u5efa2\u4e2a\u7ebf\u7a0b\u5e76\u5f00\u542f\n    pthread_t pid, cid;\n    pthread_create(&amp;pid, NULL, producer, NULL);\n    pthread_create(&amp;cid, NULL, consumer, NULL);\n    \/\/ 4\u3001\u56de\u6536\u7ebf\u7a0b\u5e76\u6ce8\u9500\u9501\u3001\u6761\u4ef6\u53d8\u91cf\n    pthread_join(pid, NULL);\n    pthread_join(cid, NULL);\n    pthread_cond_destroy(&amp;cond);\n    pthread_mutex_destroy(&amp;mutex);\n    pthread_exit(NULL);\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-cmd line-numbers\">parallels@ubuntu:~\/Linux\/pthread_syn$ .\/pthread_cp.out\nproducer msg with num = 91 | 91\nconsumer msg with num = 91 |\nproducer msg with num = 67 | 67\nconsumer msg with num = 67 |\nproducer msg with num = 36 | 36\nconsumer msg with num = 36 |\nproducer msg with num = 54 | 54\nconsumer msg with num = 54 |\nproducer msg with num = 16 | 16\n<\/code><\/pre>\n<p>\u6761\u4ef6\u53d8\u91cf\u7684\u4f18\u70b9\uff1a\u76f8\u5bf9mutex\u800c\u8a00\uff0c\u6761\u4ef6\u53d8\u91cf\u53ef\u4ee5\u51cf\u5c11\u7ade\u4e89\uff0c\u5982\u679c\u76f4\u63a5\u4f7f\u7528mutex\uff0c\u9664\u4e86\u751f\u4ea7\u8005\uff0c\u6d88\u8d39\u8005\u4e4b\u95f4\u4e5f\u8981\u7ade\u4e89\u4e92\u65a5\u91cf\uff0c\u4e8b\u5b9e\u4e0a\u5982\u679c\u6c47\u805a\u4e2d\u6ca1\u6709\u6570\u636e\uff0c\u6d88\u8d39\u8005\u4e4b\u95f4\u7684\u7ade\u4e89\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u6709\u4e86\u6761\u4ef6\u53d8\u91cf\u4e4b\u540e\uff0c\u53ea\u6709\u751f\u4ea7\u8005\u5b8c\u6210\u751f\u4ea7\uff0c\u6d88\u8d39\u8005\u4e4b\u95f4\u624d\u4f1a\u6709\u7ade\u4e89\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4ec0\u4e48\u662f\u540c\u6b65\uff1f \u540c\uff0c\u6307\u534f\u540c\u3001\u534f\u52a9\u3001\u4e92\u76f8\u914d\u5408\u3002\u4e3b\u65e8\u5728\u534f\u540c\u6b65\u8c03\uff0c\u6309\u9884\u5b9a\u7684\u5148\u540e\u6b21\u5e8f\u8fd0\u884c\u3002 \u7ebf\u7a0b\u540c\u6b65\uff0c\u6307\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u51fa\u67d0\u4e00\u529f\u80fd\u8c03\u7528\u65f6\uff0c\u5728\u6ca1\u6709\u5f97\u5230\u7ed3\u679c\u4e4b\u524d\uff0c\u8be5\u8c03\u7528\u4e0d\u8fd4\u56de\u3002\u540c\u65f6\u5176\u5b83\u7ebf\u7a0b\u4e3a\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4e0d\u80fd\u8c03\u7528\u8be5\u529f\u80fd\u3002 \u540c\u6b65\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u6570\u636e\u6df7\u4e71\uff0c\u89e3\u51b3\u4e0e\u65f6\u95f4\u6709\u5173\u7684\u9519\u8bef\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0d\u4ec5\u7ebf\u7a0b\u95f4\u9700\u8981\u540c\u6b65\uff0c\u8fdb\u7a0b\u95f4\u3001\u4fe1\u53f7\u95f4\u7b49\u90fd\u9700\u8981\u540c\u6b65\u673a\u5236\u3002 \u603b\u7ed3\uff1a\u6240\u6709\u201c\u591a\u4e2a\u63a7\u5236\u6d41\uff0c\u5171\u540c\u64cd\u4f5c\u4e00\u4e2a\u5171\u4eab\u8d44\u6e90\u201d\u7684\u60c5\u51b5\uff0c\u90fd\u9700\u8981\u540c\u6b65\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[310,196],"class_list":["post-1821","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-linux","tag-196"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}