Results 1 to 7 of 7

Thread: Calling QThread::currentThread from within boost::thread returns inconsistent results

  1. #1
    Join Date
    Jul 2015
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Dear all,

    I am working with legacy code which uses a Qt-GUI and some boost-based libraries. The application spawns some QThreads and these QThreads spawn bost::threads.
    For logging and debugging purposes I need to get the QThread IDs as well as the boost::thread IDs.
    I have written the following minimal working example to show in a more detailed fashion, what I need to do:

    Qt Code:
    1. #include <QThread>
    2. #include <boost/thread.hpp>
    3. #include <boost/thread/thread.hpp>
    4.  
    5. class mySynchronousClass
    6. {
    7. public:
    8. mySynchronousClass(std::string strFlag){
    9. if (strFlag=="Qt"){
    10. std::cout << "QThreadID (called from mySynchronousClass) is " << QThread::currentThread() << std::endl;
    11. }
    12. else if (strFlag=="boost"){
    13. std::cout << "boost::thread ID (called from mySynchronousClass) is " << boost::this_thread::get_id() << std::endl;
    14. }
    15. }
    16. };
    17.  
    18. class myTestWorker{
    19. public:
    20. myTestWorker(){}
    21. void DoWork() {
    22. std::cout << std::endl << "Start of myTestWorker::DoWork ------------------------" << std::endl;
    23. std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
    24. std::cout << "boost::thread ID is " << boost::this_thread::get_id() << std::endl;
    25. mySynchronousClass testSyncClass3("Qt");
    26. mySynchronousClass testSyncClass4("boost");
    27. std::cout << "End of myTestWorker::DoWork ------------------------" << std::endl << std::endl;
    28. }
    29. };
    30.  
    31.  
    32. class myQThread : public QThread
    33. {
    34. Q_OBJECT
    35. public:
    36. myQThread() {
    37. std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
    38.  
    39. mySynchronousClass testSyncClass("Qt");
    40.  
    41. myTestWorker w1;
    42. boost::thread BoostWorkerThread1(&myTestWorker::DoWork, &w1);
    43. BoostWorkerThread1.join();
    44. mySynchronousClass testSyncClass2("Qt");
    45. }
    46. ~myQThread(){ }
    47.  
    48. void run(){
    49. //do sth .....
    50. }
    51. };
    52.  
    53.  
    54. int main(int argc, char *argv[])
    55. {
    56. myQThread testThread;
    57. testThread.start();
    58.  
    59. system("pause");
    60. }
    To copy to clipboard, switch view to plain text mode 

    This gives the following output:
    Qt Code:
    1. QThreadID is 0000000000274850
    2. QThreadID (called from mySynchronousClass) is 0000000000274850
    3.  
    4. Start of myTestWorker::DoWork ------------------------
    5. QThreadID is 000000000029CC20
    6. boost::thread ID is 23bc
    7. QThreadID (called from mySynchronousClass) is 000000000029CC20
    8. boost::thread ID (called from mySynchronousClass) is 23bc
    9. End of myTestWorker::DoWork ------------------------
    10.  
    11. QThreadID (called from mySynchronousClass) is 0000000000274850
    To copy to clipboard, switch view to plain text mode 


    Apparently, QThread::currentThread() returns a consistent unique thread ID (in this case: 0000000000274850) as long as it is NOT called from within a boost::thread. If called from within a boost::thread the ID which is returned is changed (in this case to 000000000029CC20).
    Is it possible get a consistent QThread ID, even if called from within a boost::thread?
    A consistent QThread ID would then be (with my example from above):
    Qt Code:
    1. QThreadID is 0000000000274850
    2. QThreadID (called from mySynchronousClass) is 0000000000274850
    3.  
    4. Start of myTestWorker::DoWork ------------------------
    5. QThreadID is 0000000000274850
    6. boost::thread ID is 23bc
    7. QThreadID (called from mySynchronousClass) is 0000000000274850
    8. boost::thread ID (called from mySynchronousClass) is 23bc
    9. End of myTestWorker::DoWork ------------------------
    10.  
    11. QThreadID (called from mySynchronousClass) is 0000000000274850
    To copy to clipboard, switch view to plain text mode 
    If it is possible, how do I do it???

    Many thanks in advance and best regards

    By the way, I am using Visual Studio 11 (2012) on Windows7
    Last edited by anda_skoa; 27th August 2015 at 19:08. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    DoWork() is not executed in the context of a QThread, especially not in the context of the already existing thread.
    Why would QThread::currentThread() return the same value as an existing QThread if the current thread is not that one?

    Btw, currentThread() doesn't have any meaning out of the context of a QThread, since there is no QThread object wrapping the current system thread.
    Have you tried QThread::currentThreadId()?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Thanks for the quick reply!
    DoWork() is not executed in the context of a QThread, especially not in the context of the already existing thread.
    You are right, I should have put the call to myTestWorker::DoWork into the run method of myQThread instead of the constructor of myQThread. I corrected this mistake.

    With regard to QThread::currentThreadId() the Qt documentation says:
    "Returns the thread handle of the currently executing thread.
    Warning: The handle returned by this function is used for internal purposes and should not be used in any application code."
    Therefore I was assuming that QThread::currentThreadId() is not what I want here.

    With the following code (myTestWorker::DoWork moved into the run method of myQThread) my output now looks like (see below):

    Qt Code:
    1. class mySynchronousClass
    2. {
    3. public:
    4. mySynchronousClass(std::string strFlag){
    5. if (strFlag=="Qt"){
    6. std::cout << "QThread::currentThreadId() (called from mySynchronousClass) returns " << QThread::currentThreadId() << std::endl;
    7. std::cout << "QThread::currentThread() (called from mySynchronousClass) returns " << QThread::currentThread() << std::endl;
    8. }
    9. else if (strFlag=="boost"){
    10. std::cout << "boost::thread ID (called from mySynchronousClass) is " << boost::this_thread::get_id() << std::endl;
    11. }
    12. }
    13. };
    14.  
    15. class myTestWorker{
    16. public:
    17. myTestWorker(){}
    18. void DoWork() {
    19. std::cout << std::endl << "Start of myTestWorker::DoWork ------------------------" << std::endl;
    20. std::cout << "QThread::currentThreadId() returns " << QThread::currentThreadId() << std::endl;
    21. std::cout << "QThread::currentThread() returns " << QThread::currentThread() << std::endl;
    22. std::cout << "boost::thread ID is " << boost::this_thread::get_id() << std::endl;
    23. mySynchronousClass testSyncClass3("Qt");
    24. mySynchronousClass testSyncClass4("boost");
    25. std::cout << "End of myTestWorker::DoWork ------------------------" << std::endl << std::endl;
    26. }
    27. };
    28.  
    29.  
    30. class myQThread : public QThread
    31. {
    32. Q_OBJECT
    33. public:
    34. myQThread() {}
    35. ~myQThread(){ }
    36.  
    37. void run(){
    38. std::cout << "QThread::currentThreadId() returns " << QThread::currentThreadId() << std::endl;
    39. std::cout << "QThread::currentThread() returns " << QThread::currentThread() << std::endl;
    40.  
    41. mySynchronousClass testSyncClass("Qt");
    42.  
    43. myTestWorker w1;
    44. boost::thread BoostWorkerThread1(&myTestWorker::DoWork, &w1);
    45. BoostWorkerThread1.join();
    46. mySynchronousClass testSyncClass2("Qt");
    47. }
    48. };
    49.  
    50.  
    51.  
    52. int main(int argc, char *argv[])
    53. {
    54. QApplication app(argc, argv);
    55. myQThread testThread;
    56. testThread.start();
    57.  
    58. return app.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QThread::currentThreadId() returns 00000000000025C8
    2. QThread::currentThread() returns 000000000026FE68
    3. QThread::currentThreadId() (called from mySynchronousClass) returns 00000000000025C8
    4. QThread::currentThread() (called from mySynchronousClass) returns 000000000026FE68
    5.  
    6. Start of myTestWorker::DoWork ------------------------
    7. QThread::currentThreadId() returns 0000000000001CA4
    8. QThread::currentThread() returns 00000000003AD010
    9. boost::thread ID is 1ca4
    10. QThread::currentThreadId() (called from mySynchronousClass) returns 0000000000001CA4
    11. QThread::currentThread() (called from mySynchronousClass) returns 00000000003AD010
    12. boost::thread ID (called from mySynchronousClass) is 1ca4
    13. End of myTestWorker::DoWork ------------------------
    14.  
    15. QThread::currentThreadId() (called from mySynchronousClass) returns 00000000000025C8
    16. QThread::currentThread() (called from mySynchronousClass) returns 000000000026FE68
    To copy to clipboard, switch view to plain text mode 


    Within myTestWorker::DoWork both the ID for the boost::thread and the QThread are now 1ca4, which probably makes sense, since according to the Qt documentation
    "On Windows ... this function [i. e.QThread::currentThreadId()] returns the DWORD (Windows-Thread ID)".
    So, neither QThread::currentThreadId(), nor QThread::currentThread() can provide consistent thread IDs for myQThread.
    Does anybody have an idea how I could solve this?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Quote Originally Posted by donelron View Post
    Therefore I was assuming that QThread::currentThreadId() is not what I want here.
    You are only using it for logging, so you should be fine.

    Quote Originally Posted by donelron View Post
    So, neither QThread::currentThreadId(), nor QThread::currentThread() can provide consistent thread IDs for myQThread.
    I am not sure what you mean. You write
    Quote Originally Posted by donelron View Post
    Within myTestWorker:oWork both the ID for the boost::thread and the QThread are now 1ca4
    Same value for the same thread sounds quite consistent to me.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Same value for the same thread sounds quite consistent to me
    I was aiming for some mechanism/function that would consistently put out the same QThreadID, no matter where this function is called.
    E. g. if I call it from within a boost::thread which is "encapsulated" into a QThread then the QThread-ID is identical to the QThread-ID I receive when I call it before the boost::thread started.
    I manually edited my output so it looks like what I want it to look like:

    Qt Code:
    1. QThreadId is 00000000000025C8
    2. Start of myTestWorker::DoWork ------------------------
    3. QThreadId is 00000000000025C8
    4. boost::thread ID is 1ca4
    5. End of myTestWorker::DoWork ------------------------
    6. QThreadId is 00000000000025C8
    To copy to clipboard, switch view to plain text mode 

    In contrast to what I previously posted, QThreadId has always the same (consistent) value, i. e. 25C8. No matter, if we are inside the boost::thread or not.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Quote Originally Posted by donelron View Post
    I was aiming for some mechanism/function that would consistently put out the same QThreadID, no matter where this function is called.
    Well, obviously it matters which thread executes the function.
    The same thread has the same thread ID wherever it goes.
    A different thread has a different thread ID, it is not the same thread.

    Quote Originally Posted by donelron View Post
    E. g. if I call it from within a boost::thread which is "encapsulated" into a QThread then the QThread-ID is identical to the QThread-ID I receive when I call it before the boost::thread started.
    There is no such thing as one thread being "encapsulated" in another. All threads run in parallel, each on its own.


    I manually edited my output so it looks like what I want it to look like:

    Quote Originally Posted by donelron View Post
    In contrast to what I previously posted, QThreadId has always the same (consistent) value, i. e. 25C8.
    Exactly.
    The same thread always has the same ID.

    Quote Originally Posted by donelron View Post
    No matter, if we are inside the boost::thread or not.
    Right, the code will execute regardless of what kind of thread executes it.

    Obviously the output will be different for different threads, after all "ID" is a short hand for "identifier".
    Two threads having the same identifier would be weird.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::currentThread from within boost::thread returns inconsistent res

    Thanks for taking so much time to explain!

Similar Threads

  1. Replies: 5
    Last Post: 11th December 2014, 17:18
  2. Replies: 1
    Last Post: 4th October 2012, 14:49
  3. how to convert QThread::currentThread() to QString?
    By daemonna in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2010, 04:53
  4. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  5. Determine if QThread::currentThread is main thread?
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2008, 06:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.