6

Esiste un modo per registrare il thread o la coda su cui un metodo è in esecuzione/chiamato da? Qualcosa di simile:Registra quale coda/thread un metodo è in esecuzione su

- (void)foo 
{ 
    NSLog(@"Running on %@ queue, %@ thread", queue, thread); 
} 
+0

'[NSThread currentThread]', come visto [qui] (http://stackoverflow.com/a/1616737/1971013). –

+0

http://stackoverflow.com/a/5166740/1407017 e http://stackoverflow.com/a/1542071/1407017 – Amar

+0

Questo può aiutare: http://stackoverflow.com/a/30019141/1138755 – cromandini

risposta

9

è possibile ottenere il thread corrente con +[NSThread currentThread]. Che potrebbe avere una proprietà name, ma se non ne hai impostato uno non fare affidamento su di esso.

Le code sono più complicate perché ci sono diversi significati di "coda". Una coda potrebbe essere essere un NSOperationQueue, ed è possibile afferrare il suo name da +[NSOperationQueue currentQueue] (di nuovo, assumendo si imposta).

Quindi ci sono code di invio. È possibile ottenere la coda corrente con dispatch_get_current_queue(), ma si tenga presente che questa funzione avrà esito positivo anche se chiamata da codice non associato a una coda (!). In questo caso restituisce la coda di sfondo predefinita Le code sono etichettate, quindi puoi chiamare dispatch_queue_get_label() e se hai creato la coda con un'etichetta, la riceverai.

Quindi, in pratica, è possibile ottenere la coda o il thread, a condizione che il codice all abbia una coda di invio associata anche se non è un codice inviato. Di solito puoi anche ottenere nomi significativi per questi thread e code, il che è utile per il debug: ma è tua responsabilità chiamarli.

+0

Ottime informazioni, grazie Graham. –

1

Per ottenere il filo, è possibile utilizzare

NSLog(@"Running on %@ thread", [NSThread currentThread]); 
1

È possibile ottenere la coda spedizione corrente in questo modo:

dispatch_queue_t dispatch_get_current_queue(void); 

di testa che però ha le seguenti avvertenze:

raccomandati solo per il debug e la registrazione scopi:

il codice deve non rendere qualsiasi ipotesi sulla coda restituita, a meno che non sia una delle code globali o una coda creata dal codice stesso. Il codice deve non presupporre che l'esecuzione sincrona su una coda sia sicura da deadlock se tale coda non è quella restituita da dispatch_get_current_queue().

quindi YMMV.

2

Ecco un codice Swift che sto utilizzando attualmente. Questo si basa in parte su un'altra risposta ho già postato qui a Stack Overflow: https://stackoverflow.com/a/41294559/253938

/// Struct to contain either the thread name or an (arbitrary) thread number for the current thread. 
/// This is partly inspired by code in BaseDestination.swift in XCGLogger. Main thread is 
/// arbitrarily given thread number 0. If no thread name can be determined then the memory address 
/// of the current Thread object is arbitrarily used as the thread number. 
/// 
/// Re use of "__dispatch_queue_get_label(nil)" (seen in XCGLogger) see here: 
/// https://stackoverflow.com/questions/40186868/get-gcd-label-in-swift-3 
internal struct ThreadInfo : CustomStringConvertible { 

    var threadName : String? = nil 
    var threadNumber : Int64? = nil 


    /// Initializer. 
    public init() { 
     // Process main thread (call it thread 0) and threads whose name can be determined 
     if Thread.isMainThread { 
     threadNumber = 0 
     } 
     else if let threadName = Thread.current.name, !threadName.isEmpty { 
     self.threadName = threadName 
     } 
     else if let queueName = String(validatingUTF8: __dispatch_queue_get_label(nil)), 
     !queueName.isEmpty { 
     threadName = queueName 
     } 
     else { 
     // Convert the memory address of the current Thread object into an Int64 and use it as the 
     // (arbitrary) thread number 
     let objPtr = Unmanaged.passUnretained(Thread.current).toOpaque() 
     let onePtr = UnsafeMutableRawPointer(bitPattern: 1)! // Absolute memory location 1 
     let rawAddress : Int64 = onePtr.distance(to: objPtr) + 1 // May include high-order bits 
     threadNumber = rawAddress % (256 * 1024 * 1024 * 1024) // Remove possible high-order bits 
     } 
    } 


    /// Method to implement CustomStringConvertible, returning either the thread name if possible or 
    /// else the (arbitrary) thread number. 
    public var description: String { 
     get { 
     return 
      threadName != nil ? String(describing: threadName!) : String(describing: threadNumber!) 
     } 
    } 
} 

Per utilizzare questo solo istanziare ThreadInfo durante l'esecuzione sul thread in questione. Quindi è possibile visualizzare ThreadInfo o incorporarlo nei dati di registrazione.

let threadInfo = ThreadInfo() 
    print(threadInfo)