Swift,在GCD中,SIGUSR1不起作用

时间:2016-09-06 04:11:26

标签: swift signals grand-central-dispatch

以下尝试形成代码,但SIGUSR1的触发器不起作用。 什么是不够的?

signal(SIGUSR1, SIG_IGN);
let que = dispatch_queue_create("Que", DISPATCH_QUEUE_CONCURRENT)
let source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, UInt(SIGUSR1), 0, que)

dispatch_source_set_event_handler(source, {
    NSLog("BLOCK doing")
})

dispatch_resume(source)

sigaction(SIGUSR1, nil, nil)

1 个答案:

答案 0 :(得分:1)

研究资源" Swift中的信号处理":

  1. Kernel Architecture Overview
  2. iOS Developer Library: signal
  3. iOS Developer Library: sigaction
  4. Github IBM-Swift/BlueSignals/.../Sources/Signals.swift
  5. 代码(创建命令行工具项目)

    import Foundation
    
    var signalSource: dispatch_source_t!
    var signalOnceToken = dispatch_once_t()
    let signalID:Int32 = SIGUSR1
    
    func initSignalHandler(signal: Int32) {
        dispatch_once(&signalOnceToken) {
    
        let queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT)
            signalSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL,
                                                   UInt(signal), 0, queue)
            if let source = signalSource {
                dispatch_source_set_event_handler(source) {
                    NSLog("signal: \(signal)")
                }
                dispatch_resume(source)
            }
        }
    }
    
    initSignalHandler(signalID)
    
    sleep(1)
    
    // send signal
    Darwin.raise(signalID)
    
    NSLog("End")
    

    结果图像

    enter image description here enter image description here