{"id":513,"date":"2022-04-28T16:34:47","date_gmt":"2022-04-28T07:34:47","guid":{"rendered":"https:\/\/blog.daymore.com\/?p=513"},"modified":"2022-04-30T22:49:02","modified_gmt":"2022-04-30T13:49:02","slug":"swift-thread-%ec%82%ac%ec%9a%a9%ed%95%b4-%eb%b3%b4%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/blog.daymore.com\/?p=513","title":{"rendered":"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30"},"content":{"rendered":"\n<p>iOS \uac1c\ubc1c\uc744 \ud558\uba74\uc11c \uc2a4\ub808\ub4dc\ub97c \uc9c1\uc811 \uc0dd\uc131\ud574\uc11c \ud574\uc57c\ud558\ub294 \uc791\uc5c5\uc740 \ubcc4\ub85c \uc5c6\uc5c8\ub2e4.<br>DispatchQueue, OperationQueue \ub97c \uc774\uc6a9\ud574 \ub300\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\uc9c0\ub9cc<br>\ud2b9\uc815 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c\ub9cc \uc791\uc5c5\ub418\uae30\ub97c \uc6d0\ud55c\ub2e4\uba74 \uc2a4\ub808\ub4dc\ub97c \uc9c1\uc811 \uc0dd\uc131\ud574\uc11c \ud574\ub2f9 \uc2a4\ub808\ub4dc\uc5d0\uc11c \uc791\uc5c5\uc774 \ub418\ub3c4\ub85d \ud574\uc57c \ud55c\ub2e4.<\/p>\n\n\n\n<p>\uc571\ub0b4\uc5d0\uc11c Realm \uad00\ub828 \uc791\uc5c5\uc744 \ud588\ub294\ub370 \ud574\ub2f9 \ubaa8\ub378\uc758 Read\/Write \uac00 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c \ub418\uc5b4\uc57c \ud574\uc11c<br>\ud2b9\uc815 \uc2a4\ub808\ub4dc\ub97c \uc0dd\uc131\ud574\uc11c \uc791\uc5c5\ud574\uc57c \ud588\ub2e4.<br>\ub2e4\uc74c\uc740 \uc2a4\ub808\ub4dc\ub97c \uc0dd\uc131\ud558\uace0 \uc791\uc5c5\ub300\uc0c1\uc744 \ucd94\uac00\ud558\uc5ec \uc0dd\uc131\ub41c \uc2a4\ub808\ub4dc\uc5d0\uc11c\ub9cc \uc791\uc5c5\uc774 \ub418\ub294 \uac04\ub2e8\ud55c \uc608\uc2dc\uc774\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let t = CustomThread()\nfor i in 0 ..&lt; 5 {\n    t.push(Entry(index: i))\n}\n\nDispatchQueue.global().async {\n    for i in 5 ..&lt; 10 {\n        t.push(Entry(index: i))\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \uc2a4\ub808\ub4dc\uac00 \ucc98\ub9ac\ud560 \uc5d4\ud2b8\ub9ac\nclass Entry {\n    private var index = 0\n    \n    init(index: Int) {\n        self.index = index\n    }\n    \n    func work() {\n        print(\"working, entry index \\(index) thread \\(Thread.current)\")\n    }\n}\n<\/pre>\n\n\n\n<p>\uc2a4\ub808\ub4dc\uac00 \ucc98\ub9ac\ud560 \uc5d4\ud2b8\ub9ac\ub97c \ub123\uc744\ub54c\uc640 \uc2e4\uc81c \uc791\uc5c5\uc744 \ud558\uae30 \uc704\ud574 \uc5d4\ud2b8\ub9ac\ub97c \ube84 \ub54c<br>condition \uac1d\uccb4\ub97c \uc774\uc6a9\ud558\uc5ec \uc5d4\ud2b8\ub9ac\ub97c \ub123\ub294 \uc2a4\ub808\ub4dc\uc640 \uc5d4\ud2b8\ub9ac\ub97c \ube7c\ub294 \uc2a4\ub808\ub4dc(CustomThread) \ub3d9\uc2dc \uc811\uadfc\uc744 \ub9c9\ub294\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class CustomThread {\n    private var serialQueue = DispatchQueue(label: \"SerialQueue\")\n    private var thread: Thread!\n    private var condition = NSCondition()\n    private var entries = [Entry]()\n  \n    init() {\n        thread = Thread(target: self, selector: #selector(self.main(_:)), object: nil)\n        thread.start()\n    }\n  \n    @objc\n    func main(_ param: Any?) {\n        while !Thread.current.isCancelled {\n            condition.lock()\n            if entries.isEmpty {\n                condition.wait()\n            }\n      \n            for entry in entries {\n                entry.work()\n            }\n      \n            entries.removeAll()\n            condition.unlock()\n        }\n    }\n  \n    func push(_ entry: Entry) {\n        func push() {\n            condition.lock()\n            entries.append(entry)\n            condition.signal()\n            condition.unlock()\n        }\n    \n        serialQueue.async {\n            push()\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\uacb0\uacfc\ub294 \uc544\ub798\uc640 \uac19\ub2e4. 0~4 \uc778\ub371\uc2a4\ub294 \uba54\uc778\uc2a4\ub808\ub4dc\uc5d0\uc11c \ub123\uace0 5~9 \uc778\ub371\uc2a4\ub294 \uc784\uc758\uc758 \uc791\uc5c5 \uc2a4\ub808\ub4dc\uc5d0\uc11c \ub123\uc5c8\ub294\ub370<br>\uc2e4\uc81c \uc791\uc5c5\uc740 \ubaa8\ub450 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c \ud55c\uac1c\uc529 \uc774\ub8e8\uc5b4 \uc9c0\ub294 \uac83\uc744 \uc54c \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">working, entry index 0 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 1 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 2 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 3 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 4 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 5 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 6 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 7 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 8 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}\nworking, entry index 9 thread &lt;NSThread: 0x600000376ec0>{number = 7, name = (null)}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>iOS \uac1c\ubc1c\uc744 \ud558\uba74\uc11c \uc2a4\ub808\ub4dc\ub97c \uc9c1\uc811 \uc0dd\uc131\ud574\uc11c \ud574\uc57c\ud558\ub294 \uc791\uc5c5\uc740 \ubcc4\ub85c \uc5c6\uc5c8\ub2e4.DispatchQueue, OperationQueue \ub97c \uc774\uc6a9\ud574 \ub300\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\uc9c0\ub9cc\ud2b9\uc815 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c\ub9cc \uc791\uc5c5\ub418\uae30\ub97c \uc6d0\ud55c\ub2e4\uba74 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,23],"tags":[151,51,17,87,152,147],"class_list":["post-513","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-condition","tag-ios","tag-swift","tag-thread","tag-152","tag-147"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.daymore.com\/?p=513\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd\" \/>\n<meta property=\"og:description\" content=\"iOS \uac1c\ubc1c\uc744 \ud558\uba74\uc11c \uc2a4\ub808\ub4dc\ub97c \uc9c1\uc811 \uc0dd\uc131\ud574\uc11c \ud574\uc57c\ud558\ub294 \uc791\uc5c5\uc740 \ubcc4\ub85c \uc5c6\uc5c8\ub2e4.DispatchQueue, OperationQueue \ub97c \uc774\uc6a9\ud574 \ub300\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\uc9c0\ub9cc\ud2b9\uc815 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c\ub9cc \uc791\uc5c5\ub418\uae30\ub97c \uc6d0\ud55c\ub2e4\uba74 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.daymore.com\/?p=513\" \/>\n<meta property=\"og:site_name\" content=\"\uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-28T07:34:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-30T13:49:02+00:00\" \/>\n<meta name=\"author\" content=\"daymore\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"daymore\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.daymore.com\/?p=513\",\"url\":\"https:\/\/blog.daymore.com\/?p=513\",\"name\":\"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd\",\"isPartOf\":{\"@id\":\"https:\/\/15.165.57.189:443\/#website\"},\"datePublished\":\"2022-04-28T07:34:47+00:00\",\"dateModified\":\"2022-04-30T13:49:02+00:00\",\"author\":{\"@id\":\"https:\/\/15.165.57.189:443\/#\/schema\/person\/d2a6b2e27e0ca7aa5736172b432c1763\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.daymore.com\/?p=513#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.daymore.com\/?p=513\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.daymore.com\/?p=513#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/15.165.57.189:443\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/15.165.57.189:443\/#website\",\"url\":\"https:\/\/15.165.57.189:443\/\",\"name\":\"\uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd\",\"description\":\"blog.daymore.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/15.165.57.189:443\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/15.165.57.189:443\/#\/schema\/person\/d2a6b2e27e0ca7aa5736172b432c1763\",\"name\":\"daymore\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/15.165.57.189:443\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f7c8fbf7472c334702e25bab1089b35096ea0daf226ea3e22f66568aba3570e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f7c8fbf7472c334702e25bab1089b35096ea0daf226ea3e22f66568aba3570e6?s=96&d=mm&r=g\",\"caption\":\"daymore\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.daymore.com\/?p=513","og_locale":"ko_KR","og_type":"article","og_title":"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd","og_description":"iOS \uac1c\ubc1c\uc744 \ud558\uba74\uc11c \uc2a4\ub808\ub4dc\ub97c \uc9c1\uc811 \uc0dd\uc131\ud574\uc11c \ud574\uc57c\ud558\ub294 \uc791\uc5c5\uc740 \ubcc4\ub85c \uc5c6\uc5c8\ub2e4.DispatchQueue, OperationQueue \ub97c \uc774\uc6a9\ud574 \ub300\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\uc9c0\ub9cc\ud2b9\uc815 \ud558\ub098\uc758 \uc2a4\ub808\ub4dc\uc5d0\uc11c\ub9cc \uc791\uc5c5\ub418\uae30\ub97c \uc6d0\ud55c\ub2e4\uba74 [&hellip;]","og_url":"https:\/\/blog.daymore.com\/?p=513","og_site_name":"\uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd","article_published_time":"2022-04-28T07:34:47+00:00","article_modified_time":"2022-04-30T13:49:02+00:00","author":"daymore","twitter_card":"summary_large_image","twitter_misc":{"\uae00\uc4f4\uc774":"daymore","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.daymore.com\/?p=513","url":"https:\/\/blog.daymore.com\/?p=513","name":"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30 - \uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd","isPartOf":{"@id":"https:\/\/15.165.57.189:443\/#website"},"datePublished":"2022-04-28T07:34:47+00:00","dateModified":"2022-04-30T13:49:02+00:00","author":{"@id":"https:\/\/15.165.57.189:443\/#\/schema\/person\/d2a6b2e27e0ca7aa5736172b432c1763"},"breadcrumb":{"@id":"https:\/\/blog.daymore.com\/?p=513#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.daymore.com\/?p=513"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.daymore.com\/?p=513#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/15.165.57.189:443\/"},{"@type":"ListItem","position":2,"name":"Swift Thread \uc0ac\uc6a9\ud574 \ubcf4\uae30"}]},{"@type":"WebSite","@id":"https:\/\/15.165.57.189:443\/#website","url":"https:\/\/15.165.57.189:443\/","name":"\uae00\uc4f0\uae30, IT \uc791\uc740 \uc9c0\uc2dd","description":"blog.daymore.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/15.165.57.189:443\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Person","@id":"https:\/\/15.165.57.189:443\/#\/schema\/person\/d2a6b2e27e0ca7aa5736172b432c1763","name":"daymore","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/15.165.57.189:443\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f7c8fbf7472c334702e25bab1089b35096ea0daf226ea3e22f66568aba3570e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f7c8fbf7472c334702e25bab1089b35096ea0daf226ea3e22f66568aba3570e6?s=96&d=mm&r=g","caption":"daymore"}}]}},"_links":{"self":[{"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/posts\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=513"}],"version-history":[{"count":6,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/blog.daymore.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.daymore.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}