Results 1 to 3 of 3

Thread: ThreadID does not change

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default ThreadID does not change

    Hi,
    at the moment I try to program a TcpServer and i would like to thread the socket.
    So my code looks like this:


    Qt Code:
    1. void Server::incomingConnection(int socketId)
    2. {
    3. socket = new ClientSocket;
    4. socket->setSocketDescriptor(socketId);
    5.  
    6. QThread *socketThread = new QThread;
    7.  
    8. socket->moveToThread(socketThread);
    9. socketThread->start();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. qDebug() << QThread::currentThreadId;
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ThreadID does not change

    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?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ThreadID does not change

    Well I did the following:
    I created a timer in the Constructor of my ClientSocket:

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(displayThreadID()));
    3. timer->start(500);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ClientSocket::displayThreadID()
    2. {
    3. qDebug() << QThread::currentThreadId;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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...

Similar Threads

  1. DockWidgetArea change and layout direction change
    By mstegehu in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2012, 21:24
  2. Qt Picture change
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2010, 03:19
  3. How to change between QMainWindows???
    By webquinty in forum Newbie
    Replies: 3
    Last Post: 16th October 2009, 10:46
  4. Replies: 8
    Last Post: 15th May 2007, 09:21

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
  •  
Qt is a trademark of The Qt Company.