{"id":3738,"date":"2020-06-29T23:10:00","date_gmt":"2020-06-29T14:10:00","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3738"},"modified":"2020-06-29T23:13:05","modified_gmt":"2020-06-29T14:13:05","slug":"mutex-%ec%98%88%ec%a0%9c","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/mutex-%ec%98%88%ec%a0%9c\/","title":{"rendered":"mutex \uc608\uc81c"},"content":{"rendered":"\n<p>linux system programming, 238p. \ub450 \uc2dc\uac04 \uc0bd\uc9c8\ud588\ub2e4. thread arg \ub97c \ud3ec\uc778\ud130\ub85c \uc8fc\ub294\ub370 \uc5ec\ub7ec \uac1c\ub97c \uc4f0\ub824\uba74 struct\ub85c \uc815\uc758\ud574\uc11c \uc368\uc57c \ud55c\ub304\ub2e4. \ub9de\ub294\uc9c0 \ud2c0\ub9b0\uc9c0..<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;pthread.h>\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n\n\n\/\/arg\ub97c \uc5ec\ub7ec \ud30c\ub77c\ubbf8\ud130\ub85c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574,\n\/\/\uad6c\uc870\uccb4\ub85c \uc815\uc758\n\/\/https:\/\/stackoverflow.com\/questions\/52812004\/parameter-passing-multiple-values-using-void-pointer\n\/\/\uc5ec\uae30 \ucc38\uc870\n\/\/\uacf5\uc720 \uc790\uc6d0.\nint sharedInt=0;\n\/\/mutex \uc124\uc815.\nstatic pthread_mutex_t the_mutex = PTHREAD_MUTEX_INITIALIZER;\n\nstruct myStruct {\n\t\/\/struct\ub294 initilizer\ub97c \uc9c0\uc6d0\ud558\uc9c0 \uc54a\uc74c.\n\tchar *message;\n\tint index;\n\t\/\/index\ub97c \ubcf4\uace0 \uc785\ub825\ub41c \ud30c\ub77c\ubbf8\ud130\ub85c hello, one, two, three\ub97c \uc120\ud0dd.\n\t\/\/\uc774\ub807\uac8c \uad6c\ud604\ud558\ub824 \ud588\uc73c\ub098, \ud798\ub4e4\uc5b4 \ubb38\uc790\uc5f4\ub85c \ubcc0\uacbd.\n\t\/\/index\ub85c sharedInt\ub97c \uc5c5\ub370\uc774\ud2b8 \ud558\ub824 \ud588\uc73c\ub098,\n\t\/\/\uc798 \uc548\ub428..\n\t\/\/\uc2a4\ub808\ub4dc\uac00 \uc2dc\uc791\ud560 \ub54c\ub9c8\ub2e4 sharedInt\ub97c \uc5c5\ub370\uc774\ud2b8 \ud558\uba74 \uae30\uc874 \uac12\uc744\n\t\/\/\ubc84\ub9ac\uace0 \ub2e4\uc2dc \uc500!!\n\t\/\/thread \uc548\uc5d0\uc11c\ub9cc \uacf5\uc720 \uc790\uc6d0\uc744 \uc870\uc791..\n\n\n};\n\n\n\nvoid* start_routine(void* arg ){\n\tint pid;\n\tint threadID;\n\t\/\/struct\ub85c arg\ub97c \ubc1b\uae30 \uc704\ud574, \n\t\/\/struct\ub85c \ubcc0\uacbd.\n\tstruct myStruct *temp;\n\n\ttemp = arg;\n\t\/\/pointer \ub85c global \ubcc0\uc218 \ubcc0\uacbd\n\n\t\/\/pthread_mutex_lock(&amp;the_mutex);\n\t\/\/sharedInt = temp->index;\n\n\t\/\/printf(\"my thread sentence is \\\"%s\\\"\\n\", temp->message);\n\t\/\/printf(\"temp->index is %d\\n\", temp->index);\n\n\tpthread_mutex_lock(&amp;the_mutex);\n\n\tprintf(\"SharedInt\ub294 %d\\n\",sharedInt);\n\tif(sharedInt == 0){\n\t\t\/\/strsep(temp->message, \" \");\n\t\t\/\/printf(\"my thread word is %s\\n\",outputword);\n\t\tprintf(\"zero\\n\");\n\t\t\/\/temp->index++;\n\n\t}\n\n\telse if(sharedInt == 1){\n\t\t\/\/printf(\"my thread word is %s\\n\",outputword);\n\t\tprintf(\"one\\n\");\n\t\t\/\/temp->index++;\n\n\t}\n\telse if(sharedInt == 2){\n\t\tprintf(\"two\\n\");\n\t\t\/\/temp->index++;\n\n\t}\n\n\telse{\n\t\tprintf(\"three or more\\n\");\n\t}\n\tsharedInt++;\n\n\tpthread_mutex_unlock(&amp;the_mutex);\n\n\t}\n\nint main(){\n\n\tpthread_t thread[2];\n\tint ret, errorno;\n\tstruct myStruct message = {\"hello one two three\", 0 };\n\tprintf(\"myStruct message index\ub294 %d\\n\", message.index);\n\t\/\/\uc544\ub798 \ubd80\ubd84\uc740 \ubd88\ud544\uc694 \ubd80\ubd84. thread \ubc16\uc5d0\uc11c \uc870\uc791\ud558\uc5ec \ubb34\uc758\ubbf8\ud568.\n\t\/\/message.index=2;\n\n\tret = pthread_create(&amp;thread[0],NULL,start_routine,(void*)&amp;message);\n\n\tif(ret&lt;0){\n\t\terrorno = ret;\n\t\t\/\/printf(\"%d\\n\",errorno);\n\t\tperror(\"pthread_create\");\n\t\treturn -1;\n\t}\n\t\/\/\uc544\ub798 \ubd80\ubd84\uc740 \ubd88\ud544\uc694 \ubd80\ubd84. thread \ubc16\uc5d0\uc11c \uc870\uc791\ud558\uc5ec \ubb34\uc758\ubbf8\ud568.\n\t\/\/message.index=1;\n\n\t\/\/printf(\"myStruct message index\ub294 %d\\n\", message.index);\n\n\t\/\/printf(\"struct \ucd9c\ub825: %d\",message.index);\n\t\/\/printf(\"message \ud655\uc778 %d\",message.index);\n\n\tret = pthread_create(&amp;thread[1],NULL,start_routine,(void*)&amp;message);\n\n\tif(ret&lt;0){\n\t\terrorno = ret;\n\t\t\/\/printf(\"%d\\n\",errorno);\n\t\tperror(\"pthread_create\");\n\t\treturn -1;\n\t}\n\n\t\/\/\uac01 \uc2a4\ub808\ub4dc \ub05d\ub098\uae38 \ub300\uae30\n\n\tpthread_join(thread[0],NULL);\n\tpthread_join(thread[1],NULL);\n\t\/\/mutex \uc0ad\uc81c\n\tpthread_mutex_destroy(&amp;the_mutex);\n\tprintf(\"completed\\n\");\n\treturn 0;\n\n}\n<\/pre>\n\n\n\n<p>100\ubc88 \ub3cc\ub9ac\uba74 mutex lock\uc744 \uc124\uc815\ud55c \uacbd\uc6b0\uc640 \uc124\uc815\ud558\uc9c0 \uc54a\uc740 \uacbd\uc6b0 \uac12\uc774 \ub2e4\ub984. \ub098\ub984 \uc774\ud574\ud588\ub2e4\uace0 \ubbff\uace0 \uc2f6\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux system programming, 238p. \ub450 \uc2dc\uac04 \uc0bd\uc9c8\ud588\ub2e4. thread arg \ub97c \ud3ec\uc778\ud130\ub85c \uc8fc\ub294\ub370 \uc5ec\ub7ec \uac1c\ub97c \uc4f0\ub824\uba74 struct\ub85c \uc815\uc758\ud574\uc11c \uc368\uc57c \ud55c\ub304\ub2e4. \ub9de\ub294\uc9c0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12],"tags":[775,53,776],"class_list":["post-3738","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-linux","tag-system-program"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=3738"}],"version-history":[{"count":4,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3738\/revisions"}],"predecessor-version":[{"id":3742,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3738\/revisions\/3742"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}