{"id":4660,"date":"2022-02-05T17:25:59","date_gmt":"2022-02-05T08:25:59","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4660"},"modified":"2022-02-12T14:29:14","modified_gmt":"2022-02-12T05:29:14","slug":"c-thread-%ec%82%ac%ec%9a%a9%eb%b2%95","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4660","title":{"rendered":"C Thread \uc0ac\uc6a9\ubc95"},"content":{"rendered":"<h1>C Thread \uc0ac\uc6a9\ubc95<\/h1>\n<p>Linux pthread \ub97c \uc774\uc6a9\ud574 \uc2a4\ub798\ub4dc\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\ub2e4.<\/p>\n<h2>Thread \uc0dd\uc131<\/h2>\n<pre><code class=\"language-c\">#include &lt;pthread.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n\nvoid *t1_func(void *data) {\n    pid_t pid;\n    pthread_t tid;\n\n    pid = getpid();\n    tid = pthread_self();\n\n    char *thread_name = (char *) data;\n\n    for (int i = 0; i &lt; 10; i++) {\n        printf(&quot;[%s] pid: %u, tid: %x --- %d\\n&quot;, thread_name, (unsigned int) pid, (unsigned int) tid, i);\n        sleep(1);\n    }\n}\n\nint main() {\n    pthread_t p_thread;\n    int t_id;\n    int t_status;\n\n    char *p1 = &quot;thread_1&quot;;\n    char *p_main = &quot;thread_main&quot;;\n\n    t_id = pthread_create(&amp;p_thread, NULL, t1_func, (void *) p1);\n    if (t_id &lt; 0) {\n        perror(&quot;thread create error&quot;);\n        exit(1);\n    }\n\n    \/\/ main thread \uc640 \uc0dd\uc131\ud55c thread \uac00 \ub3d9\uc2dc\uc5d0 \uc2e4\ud589\ub41c\ub2e4.\n    t1_func((void *) p_main);\n\n    \/\/ \uc0dd\uc131\ud55c \uc2a4\ub798\ub4dc\uc758 \uc885\ub8cc\ub97c \uae30\ub2e4\ub9b0\ub2e4.\n    pthread_join(p_thread, (void **) &amp;t_status);\n\n    printf(&quot;finished.\\n&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">gcc main.c -lpthread\n\n.\/a.out\n[thread_main] pid: 1270690, tid: e21c7740 --- 0\n[thread_1] pid: 1270690, tid: e21c6700 --- 0\n[thread_main] pid: 1270690, tid: e21c7740 --- 1\n[thread_1] pid: 1270690, tid: e21c6700 --- 1\n[thread_main] pid: 1270690, tid: e21c7740 --- 2\n[thread_1] pid: 1270690, tid: e21c6700 --- 2\n[thread_main] pid: 1270690, tid: e21c7740 --- 3\n[thread_1] pid: 1270690, tid: e21c6700 --- 3\n[thread_main] pid: 1270690, tid: e21c7740 --- 4\n[thread_1] pid: 1270690, tid: e21c6700 --- 4\n[thread_main] pid: 1270690, tid: e21c7740 --- 5\n[thread_1] pid: 1270690, tid: e21c6700 --- 5\n[thread_main] pid: 1270690, tid: e21c7740 --- 6\n[thread_1] pid: 1270690, tid: e21c6700 --- 6\n[thread_main] pid: 1270690, tid: e21c7740 --- 7\n[thread_1] pid: 1270690, tid: e21c6700 --- 7\n[thread_main] pid: 1270690, tid: e21c7740 --- 8\n[thread_1] pid: 1270690, tid: e21c6700 --- 8\n[thread_main] pid: 1270690, tid: e21c7740 --- 9\n[thread_1] pid: 1270690, tid: e21c6700 --- 9\nfinished.<\/code><\/pre>\n<h2>Thread \uac04 \ubcc0\uc218 \uacf5\uc720<\/h2>\n<p>thread \ub294 \ud558\ub098\uc758 \ud504\ub85c\uc138\uc2a4 \uc0c1\uc5d0\uc11c \uba54\ubaa8\ub9ac\ub97c \uacf5\uc720\ud558\uba70 \uc2e4\ud589\ub41c\ub2e4.<br \/>\n\ub530\ub77c\uc11c, \uacf5\uc720 \ubcc0\uc218\uc5d0 \ub300\ud55c \ubcc0\uc218\ubcc0\uacbd\uc740 \ub9e4\uc6b0 \uc27d\ub2e4.<br \/>\n\ud558\uc9c0\ub9cc, \ud558\ub098\uc758 thread \uc5d0 \uc804\ub2ec\ub41c \ubcc0\uc218\uac00 \uc5b4\ub514\uc11c \ubcc0\uacbd\ub418\uc5c8\ub294\uc9c0 \ucd94\uc801\uc774 \ub9e4\uc6b0 \uc5b4\ub835\ub2e4.<\/p>\n<h2>Thread \uac04 \ubcc0\uc218 \uc7a0\uae08<\/h2>\n<h3>pthread_mutex_t<\/h3>\n<p>thread \uc2e4\ud589\uc758 \uc21c\uc11c \ubcf4\uc7a5\uc774 \ud544\uc694\uc5c6\ub294 \uacbd\uc6b0 \uc0ac\uc6a9\ud55c\ub2e4.<br \/>\n\ud55c\ubc88\uc5d0 \ud558\ub098\uc758 thread \ub9cc \uc2e4\ud589\ub41c\ub2e4.<\/p>\n<pre><code class=\"language-c\">pthread_mutex_t mutex_lock;\n\n\/\/ \ub2e4\ub978 \ubaa8\ub4e0 thread \ub97c \uba48\ucd94\uace0 \ud558\ub098\uc758 thread \ub9cc \uc2e4\ud589\ub41c\ub2e4.\npthread_mutex_lock(&amp;mutex_lock);\n\npthread_mutex_unlock(&amp;mutex_lock);<\/code><\/pre>\n<h3>semaphore<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>C Thread \uc0ac\uc6a9\ubc95 Linux pthread \ub97c \uc774\uc6a9\ud574 \uc2a4\ub798\ub4dc\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\ub2e4. Thread \uc0dd\uc131 #include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; void *t1_func(void *data) { pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); char *thread_name = (char *) data; for (int i = 0; i &lt; 10; i++) { printf(&quot;[%s] pid: %u, tid:\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4660\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4660","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4660"}],"version-history":[{"count":5,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4660\/revisions"}],"predecessor-version":[{"id":4720,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4660\/revisions\/4720"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}