{"id":2097,"date":"2014-11-21T05:28:27","date_gmt":"2014-11-20T20:28:27","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=2097"},"modified":"2017-01-31T19:42:17","modified_gmt":"2017-01-31T10:42:17","slug":"c11-stdthread","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=2097","title":{"rendered":"[C++11] std::thread"},"content":{"rendered":"<p><P>\uae30\uc874\uc758 C++\uc5d0\uc11c\uc758 Thread\ub294 \uc6b4\uc601\uccb4\uc81c\uc5d0 \uc885\uc18d\ub418\ub294 API\ub85c \uc81c\uacf5\ub418\uac70\ub098 \ube44\ud45c\uc900\uc774 \uc544\ub2cc \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \ud1b5\ud574 \uc81c\uacf5\ub418\ub2e4\uac00 C++11\uc5d0\uc11c \uc644\uc804\ud55c \ud45c\uc900\uc73c\ub85c\uc368 \uc790\ub9ac\uc7a1\uc544 \ub2e8\uc77c API\ub85c \ub3d9\uc77c\ud55c \ucf54\ub4dc\ub85c \uac1c\ubc1c\uc774 \uac00\ub2a5\ud558\uac8c \ub418\uc5c8\uc2b5\ub2c8\ub2e4. \uc544\ub798\uc758 \ucf54\ub4dc\ub294 C++11\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 std::thread\ub97c \uc0ac\uc6a9\ud55c \uac04\ub2e8\ud55c \uc608\uc785\ub2c8\ub2e4.<\/P><\/p>\n<pre>\r\n#include \"stdafx.h\"\r\n#include <iostream>\r\n#include <thread>\r\n\r\nusing namespace std;\r\n\r\nvoid thread_function(int tid) {\r\n    cout << \"abcdefghijklmnopqrstuvwxyz\" << endl;\r\n}\r\n\r\nint _tmain(int argc, _TCHAR* argv[])\r\n{\r\n    const int cntThreads = 100000;\r\n    thread t[cntThreads];\r\n\r\n    for(int i = 0; i < cntThreads; ++i) {\r\n        t[i] = thread(thread_function, i);\r\n    }\r\n\r\n    for(int i = 0; i < cntThreads; ++i) {\r\n        t[i].join();\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p><P>\uc704\uc758 \ucf54\ub4dc\ub294 10\ub9cc\uac1c\uc758 \uc2a4\ub808\ub4dc\ub97c \uc0dd\uc131\ud558\uace0 \uc788\uace0 \uac01 \uc2a4\ub808\ub4dc\ub294 7\ubc88 \ucf54\ub4dc\uc5d0\uc11c \uc815\uc758\ud55c thread_function \ud568\uc218\ub97c \uc2e4\ud589\ud569\ub2c8\ub2e4. 17\ubc88 \ucf54\ub4dc\uac00 \uc2a4\ub808\ub4dc\ub97c \uc0dd\uc131\ud558\uace0 \uc2e4\ud589\ud558\ub294 \ucf54\ub4dc\ub85c \uccab\ubc88\uc9f8 \uc778\uc790\ub294 \uc2e4\ud589\ud560 \ud568\uc218\uc774\uace0 \ub450\ubc88\uc9f8\ub294 \uc2a4\ub808\ub4dc\uc758 id\uc785\ub2c8\ub2e4. \uadf8\ub9ac\uace0 21\ubc88 \ucf54\ub4dc\ub294 \uc2a4\ub808\ub4dc\uac00 \uc644\uc804\ud788 \uc885\ub8cc\ub420\ub54c\uae4c\uc9c0 \uc8fc \uc2a4\ub808\ub4dc\ub97c \ub300\uae30\uc2dc\ud0a4\ub294 join \ud568\uc218\uc785\ub2c8\ub2e4. \uc704\uc758 \ucf54\ub4dc\ub97c \uc2e4\ud589\ud574 \ubcf4\uba74 10\ub9cc\uac1c\uc758 \uc2a4\ub808\ub4dc\uac00 abc..... \ubb38\uc790\uc5f4\uc774 \uc11c\ub85c \ub4a4\uc11e\uc5ec \ud45c\uc2dc\ub418\ub294 \uac83\uc744 \ubcfc \uc218 \uc788\ub294\ub370, \uc774\ub97c \uc704\ud574 \ub3d9\uae30\ud654\ub97c \ud574\uc904 \ud544\uc694\uac00 \uc788\uc2b5\ub2c8\ub2e4. \ub2e4\uc74c \ucf54\ub4dc\ub294 \uc774\ub7ec\ud55c \ub3d9\uae30\ud654\ub97c \uc704\ud574 C++11\uc5d0\uc11c \uc81c\uacf5\ub418\ub294 std::mutex\ub97c \uc0ac\uc6a9\ud55c \uc608\uc81c\uc785\ub2c8\ub2e4.<\/P><\/p>\n<pre>\r\n#include \"stdafx.h\"\r\n#include <iostream>\r\n#include <thread>\r\n#include <mutex>\r\n\r\nusing namespace std;\r\n\r\nmutex m;\r\n\r\nvoid thread_function(int tid) {\r\n    m.lock();\r\n\r\n    cout << \"abcdefghijklmnopqrstuvwxyz\" << endl;\r\n\r\n    m.unlock();\r\n}\r\n\r\nint _tmain(int argc, _TCHAR* argv[])\r\n{\r\n    const int cntThreads = 100000;\r\n    thread t[cntThreads];\r\n\r\n    for(int i = 0; i < cntThreads; ++i) {\r\n        t[i] = thread(thread_function, i);\r\n    }\r\n\r\n    for(int i = 0; i < cntThreads; ++i) {\r\n        t[i].join();\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p><P>\ubcc0\uacbd\ub41c \ubd80\ubd84\uc740 8\ubc88 \ucf54\ub4dc\uc5d0\uc11c \ub3d9\uae30\ud654\ub97c \uc704\ud55c mutex \uac1d\uccb4\ub97c \ud558\ub098\ub97c \uc815\uc758\ud588\uace0 \uc2a4\ub808\ub4dc \ud568\uc218 \ub0b4\uc5d0\uc11c abc.... \ubb38\uc790\uc5f4\uc744 \ud45c\uc2dc\ud558\ub294 \ubd80\ubd84\uc744 \ub3d9\uae30\ud654 \ud558\uae30 \uc704\ud574 \uc774 \ucf54\ub4dc\uc758 \uc55e\ub4a4\uc5d0 mutex\uc758 lock\uacfc unlock \ud568\uc218\ub97c \ud638\ucd9c\ud558\uace0 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub807\uac8c \ud558\uba74 \uc774\ub4e4 \ucf54\ub4dc \uc0ac\uc774\uc5d0 \uc788\ub294 \ucf54\ub4dc\uac00 \ub3d9\uae30\ud654\uac00 \ub418\uc5b4 \ubb38\uc790\uc5f4\uc774 \ub4a4\uc11e\uc774\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uae30\uc874\uc758 C++\uc5d0\uc11c\uc758 Thread\ub294 \uc6b4\uc601\uccb4\uc81c\uc5d0 \uc885\uc18d\ub418\ub294 API\ub85c \uc81c\uacf5\ub418\uac70\ub098 \ube44\ud45c\uc900\uc774 \uc544\ub2cc \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \ud1b5\ud574 \uc81c\uacf5\ub418\ub2e4\uac00 C++11\uc5d0\uc11c \uc644\uc804\ud55c \ud45c\uc900\uc73c\ub85c\uc368 \uc790\ub9ac\uc7a1\uc544 \ub2e8\uc77c API\ub85c \ub3d9\uc77c\ud55c \ucf54\ub4dc\ub85c \uac1c\ubc1c\uc774 \uac00\ub2a5\ud558\uac8c \ub418\uc5c8\uc2b5\ub2c8\ub2e4. \uc544\ub798\uc758 \ucf54\ub4dc\ub294 C++11\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 std::thread\ub97c \uc0ac\uc6a9\ud55c \uac04\ub2e8\ud55c \uc608\uc785\ub2c8\ub2e4. #include &#8220;stdafx.h&#8221; #include #include using namespace std; void thread_function(int tid) { cout<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,8],"tags":[14],"class_list":["post-2097","post","type-post","status-publish","format-standard","hentry","category-cpp","category-programming","tag-c"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2097"}],"version-history":[{"count":1,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2097\/revisions"}],"predecessor-version":[{"id":2822,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2097\/revisions\/2822"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}