{"id":15037879,"url":"https://github.com/queeniecplusplus/ios_back_9","last_synced_at":"2026-01-27T06:35:22.159Z","repository":{"id":104588320,"uuid":"333631459","full_name":"QueenieCplusplus/iOS_Back_9","owner":"QueenieCplusplus","description":"using Semaphore to show signal for controlling the same Thread","archived":false,"fork":false,"pushed_at":"2021-01-28T03:24:00.000Z","size":1170,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T13:13:46.356Z","etag":null,"topics":["async","dispatchqueue","ios11","ios14","semaphore","signal","swift4","swift5","xcode11","xcode12"],"latest_commit_sha":null,"homepage":"https://github.com/QueenieCplusplus/QuickGoThru/blob/master/README.md#apple-ios-swift-溫故愛鳳---計畫復甦","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QueenieCplusplus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-28T03:13:56.000Z","updated_at":"2021-01-28T03:24:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"1e0e1063-9f56-4241-a9af-a0dab9b862e2","html_url":"https://github.com/QueenieCplusplus/iOS_Back_9","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FiOS_Back_9","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FiOS_Back_9/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FiOS_Back_9/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FiOS_Back_9/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QueenieCplusplus","download_url":"https://codeload.github.com/QueenieCplusplus/iOS_Back_9/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243410445,"owners_count":20286395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","dispatchqueue","ios11","ios14","semaphore","signal","swift4","swift5","xcode11","xcode12"],"created_at":"2024-09-24T20:36:11.282Z","updated_at":"2026-01-27T06:35:22.121Z","avatar_url":"https://github.com/QueenieCplusplus.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# iOS_Back_9\nusing Semaphore to show signal for controlling the same Thread\n\n1. code.\n\n        //\n        //  ViewController.swift\n        //  KatesOnlyOneThreadApp\n        //\n        //  Created by KatesAndroid on 2021/1/28 AM 10:40\n        //\n        // 目標：指定如下作業僅能由同一執行緒完成。\n        // control one thread using semaphore 號誌。\n        // semaphore 可產生 signal 信號。\n\n        import UIKit\n\n        class ViewController: UIViewController {\n\n            override func viewDidLoad() {\n                super.viewDidLoad()\n\n                // 產生多執行緒\n                // global 不放參處預設為 default\n                // 權限介於 UI 和 Backgound 之間\n                // 方法有非同步與同步兩種\n                DispatchQueue.global().async {\n\n                    for i in 1 ... 20 {\n\n                        // 號誌即信號燈\n                        let s = DispatchSemaphore(value: 0)\n\n                        if i == 5 {\n\n                            DispatchQueue.main.async(execute:\n\n                                {self.Alerter(ds: s)}\n\n                            )\n\n                        }else{\n\n                            // 釋放信號\n                            s.signal()\n                        }\n\n                        _ = s.wait(timeout: DispatchTime.distantFuture)\n                        print(i)\n                        sleep(1)\n\n                    }\n\n                }\n\n            }\n\n\n            func Alerter(ds: DispatchSemaphore) {\n\n\n                // 提醒彈出\n                let showAlert = UIAlertController(title: \"溫馨提醒\", message: \"您可點擊OK後繼續作業，完成任務。\", preferredStyle: .alert)\n\n                // 提醒中的按鈕\n                let afterOK = UIAlertAction(title: \"OK\", style: .default){\n\n                    (todo) in\n                    ds.signal()\n                    self.dismiss(animated: true, completion: nil)\n\n                }\n\n                // 為提醒加上按鈕\n                showAlert.addAction(afterOK)\n\n                // 彈出提醒\n                present(showAlert, animated: true)\n\n            }\n\n        }\n\n\n2. output \n\n   ![](https://raw.githubusercontent.com/QueenieCplusplus/iOS_Back_9/main/thread%20num%20%3D%205.png)\n   \n   ![](https://raw.githubusercontent.com/QueenieCplusplus/iOS_Back_9/main/siganl%20show.png)\n\n3. iOS' tip (log)\n\n        NSLog(\"\")\n        \n        sleep()\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueeniecplusplus%2Fios_back_9","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqueeniecplusplus%2Fios_back_9","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueeniecplusplus%2Fios_back_9/lists"}