0

I have a threadSafe method that gets called by multiple threads. Is there a way to know which thread called it?

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
yan bellavance
  • 4,710
  • 20
  • 62
  • 93
  • Most of the time, it's a bad idea to change the behavior of a method depending on the identity of the caller. What are you trying to do that should change depending on which thread the caller is running in? If it's reasonable to need to know that, then there is usually something obviously related to the question to pass in. – PanCrit Feb 25 '10 at 18:59
  • Basically, I have functionA which uses a QUDPSocket to writeDatagram and the readyRead signal is connected to a slot that readDatagram. When functionA is called from the mainthread I dont want the signal to be emitted or the method to be called(i use readyread directly) but I do when it is called from the qthread. The udp socket is in the main thread. – yan bellavance Feb 25 '10 at 22:05
  • Can't you break functionA in 2 or more functions? Then the main thread only call the "necessary part" and the other ones call the full function? – Samuel Carrijo Feb 25 '10 at 22:35
  • I dont emit the readyRead signal I only call the writeDatagram function which eventually gets a signal emitted from the event generated by arrival of the reply packet. So from a qthread the slot will get executed while functionA is running but from the mainthread it wont because the slot wont get called until the function returns. this is related to event loops – yan bellavance Feb 25 '10 at 23:11

2 Answers2

3

Well, you know the thread that calls the method, and by extension the same thread will be active inside that method call. You can just call QThread::currentThread() to get this.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • actually there is no method called getCurrentThread but the there is a static in QThread called currentThread...ill try it. – yan bellavance Feb 26 '10 at 00:41
  • Ah yes, sorry -- I thought the original post said Java :) I got currentThread wrong in Java too, it is in fact Thread.currentThread() here too, ugh :) Anyway, glad my post helped; looks like the two APIs have similarly named classes / methods anyway :) – Chris Dennett Feb 26 '10 at 00:58
  • That didn't work for me. The docs say you are not supposed to emit finished() yourself. I had an array of QThread *, and compared them to `QThread::currentThread()`, when finished() was called, but there was no match. I think it returns the GUI thread when signal finished() is called without overriding or emitting it. – CodeLurker Oct 29 '18 at 06:08
1

If you need it, you could add a threadId parameter, and let it thread pass it to the method

Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
  • yeah I was aware of that but I wanted to minimize user intervention so as to not let anyone call the function in the wrong way, now user has to know if its a mainthread or qthread call. So it would have to add that parameter only when working in the qthread – yan bellavance Feb 25 '10 at 22:18
  • The docs say that is an internal value, and you're not supposed to use it. – CodeLurker Oct 29 '18 at 06:09