donelron
27th August 2015, 12:20
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:
#include <QThread>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
class mySynchronousClass
{
public:
mySynchronousClass(std::string strFlag){
if (strFlag=="Qt"){
std::cout << "QThreadID (called from mySynchronousClass) is " << QThread::currentThread() << std::endl;
}
else if (strFlag=="boost"){
std::cout << "boost::thread ID (called from mySynchronousClass) is " << boost::this_thread::get_id() << std::endl;
}
}
};
class myTestWorker{
public:
myTestWorker(){}
void DoWork() {
std::cout << std::endl << "Start of myTestWorker::DoWork ------------------------" << std::endl;
std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
std::cout << "boost::thread ID is " << boost::this_thread::get_id() << std::endl;
mySynchronousClass testSyncClass3("Qt");
mySynchronousClass testSyncClass4("boost");
std::cout << "End of myTestWorker::DoWork ------------------------" << std::endl << std::endl;
}
};
class myQThread : public QThread
{
Q_OBJECT
public:
myQThread() {
std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
mySynchronousClass testSyncClass("Qt");
myTestWorker w1;
boost::thread BoostWorkerThread1(&myTestWorker::DoWork, &w1);
BoostWorkerThread1.join();
mySynchronousClass testSyncClass2("Qt");
}
~myQThread(){ }
void run(){
//do sth .....
}
};
int main(int argc, char *argv[])
{
myQThread testThread;
testThread.start();
system("pause");
}
This gives the following output:
QThreadID is 0000000000274850
QThreadID (called from mySynchronousClass) is 0000000000274850
Start of myTestWorker::DoWork ------------------------
QThreadID is 000000000029CC20
boost::thread ID is 23bc
QThreadID (called from mySynchronousClass) is 000000000029CC20
boost::thread ID (called from mySynchronousClass) is 23bc
End of myTestWorker::DoWork ------------------------
QThreadID (called from mySynchronousClass) is 0000000000274850
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):
QThreadID is 0000000000274850
QThreadID (called from mySynchronousClass) is 0000000000274850
Start of myTestWorker::DoWork ------------------------
QThreadID is 0000000000274850
boost::thread ID is 23bc
QThreadID (called from mySynchronousClass) is 0000000000274850
boost::thread ID (called from mySynchronousClass) is 23bc
End of myTestWorker::DoWork ------------------------
QThreadID (called from mySynchronousClass) is 0000000000274850
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
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:
#include <QThread>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
class mySynchronousClass
{
public:
mySynchronousClass(std::string strFlag){
if (strFlag=="Qt"){
std::cout << "QThreadID (called from mySynchronousClass) is " << QThread::currentThread() << std::endl;
}
else if (strFlag=="boost"){
std::cout << "boost::thread ID (called from mySynchronousClass) is " << boost::this_thread::get_id() << std::endl;
}
}
};
class myTestWorker{
public:
myTestWorker(){}
void DoWork() {
std::cout << std::endl << "Start of myTestWorker::DoWork ------------------------" << std::endl;
std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
std::cout << "boost::thread ID is " << boost::this_thread::get_id() << std::endl;
mySynchronousClass testSyncClass3("Qt");
mySynchronousClass testSyncClass4("boost");
std::cout << "End of myTestWorker::DoWork ------------------------" << std::endl << std::endl;
}
};
class myQThread : public QThread
{
Q_OBJECT
public:
myQThread() {
std::cout << "QThreadID is " << QThread::currentThread() << std::endl;
mySynchronousClass testSyncClass("Qt");
myTestWorker w1;
boost::thread BoostWorkerThread1(&myTestWorker::DoWork, &w1);
BoostWorkerThread1.join();
mySynchronousClass testSyncClass2("Qt");
}
~myQThread(){ }
void run(){
//do sth .....
}
};
int main(int argc, char *argv[])
{
myQThread testThread;
testThread.start();
system("pause");
}
This gives the following output:
QThreadID is 0000000000274850
QThreadID (called from mySynchronousClass) is 0000000000274850
Start of myTestWorker::DoWork ------------------------
QThreadID is 000000000029CC20
boost::thread ID is 23bc
QThreadID (called from mySynchronousClass) is 000000000029CC20
boost::thread ID (called from mySynchronousClass) is 23bc
End of myTestWorker::DoWork ------------------------
QThreadID (called from mySynchronousClass) is 0000000000274850
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):
QThreadID is 0000000000274850
QThreadID (called from mySynchronousClass) is 0000000000274850
Start of myTestWorker::DoWork ------------------------
QThreadID is 0000000000274850
boost::thread ID is 23bc
QThreadID (called from mySynchronousClass) is 0000000000274850
boost::thread ID (called from mySynchronousClass) is 23bc
End of myTestWorker::DoWork ------------------------
QThreadID (called from mySynchronousClass) is 0000000000274850
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