PDA

View Full Version : ThreadID does not change



Bernie
17th February 2011, 10:43
Hi,
at the moment I try to program a TcpServer and i would like to thread the socket.
So my code looks like this:




void Server::incomingConnection(int socketId)
{
socket = new ClientSocket;
socket->setSocketDescriptor(socketId);

QThread *socketThread = new QThread;

socket->moveToThread(socketThread);
socketThread->start();

}


With this code I expected two threads. One main thread and another for my ClientSocket. But using



qDebug() << QThread::currentThreadId;


returns always the same ThreadID (in Server and ClientSocket class).

What am I doing wrong? Or am I using currentThreadId wrong?
Thanks for your help.

Bernie

franz
17th February 2011, 12:46
If QThread::currentThreadId() returns the same thread id every time, you're calling it from the same thread every time. How did you setup the code path to this call?

Bernie
17th February 2011, 16:19
Well I did the following:
I created a timer in the Constructor of my ClientSocket:


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(displayThreadID()));
timer->start(500);


void ClientSocket::displayThreadID()
{
qDebug() << QThread::currentThreadId;
}

The ID isn't different from the ID in the main Thread, before creating the Thread for the socket.

At the moment my applications starts crashing sometimes because of thread errors... :(