Record which queue / thread method is running

Is there a way to register the thread or queue from which the method is started / called? Sort of:

- (void)foo { NSLog(@"Running on %@ queue, %@ thread", queue, thread); } 
+6
source share
4 answers

You can get the current thread with +[NSThread currentThread] . This may have a name property, but if you haven't set it, don't rely on it.

Queues are more complicated because there are different "queue" values. The queue can be an NSOperationQueue , and you can get its name from +[NSOperationQueue currentQueue] (again, if you set it).

Then there are the dispatch queues. You can get the current queue using dispatch_get_current_queue() , but you should warn that this function will succeed even if it is called from code that is not associated with the queue (!). In this case, it returns the default background queues. The queues are marked, so you can call dispatch_queue_get_label() , and if you created a queue with a label, you will get this.

So, in principle, yes, you can get a queue or thread - provided that all the code has an associated dispatch queue, even if it is not the code that was sent. You can also get meaningful names for these threads and queues, which is convenient for debugging: but you are responsible for their name.

+9
source

Here is the Swift code I'm using now. This is partly based on another answer that I previously posted here on Stack Overflow: fooobar.com/questions/62298 / ...

 /// 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!) } } } 

To use this, simply create a ThreadInfo while working on the thread in question. You can then display ThreadInfo or embed it in the logging data.

  let threadInfo = ThreadInfo() print(threadInfo) 
+2
source

To get the stream, you can use

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

You can get the current dispatch queue as follows:

 dispatch_queue_t dispatch_get_current_queue(void); 

But the header has the following warnings:

Recommended only for debugging and logging:

The code should not make any assumptions about the returned queue, unless it is one of the global queues or a queue created by the code itself. The code should not assume that synchronous execution in a queue is safe deadlock if that queue is not the one returned by dispatch_get_current_queue ().

therefore YMMV.

+1
source

Source: https://habr.com/ru/post/945405/


All Articles