Results 1 to 2 of 2

Thread: Multithreaded server

  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multithreaded server

    I created two classes of FServer heir of QTcpServer and FThread from QThread. When you connect a new customer creates a separate thread, and it passes the handle to the socket.

    Qt Code:
    1. void FServer::incomingConnection(int socketDescriptor)
    2. {
    3. FThread *thread = new FThread(socketDescriptor, this);
    4. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    5. connect(thread, SIGNAL(createNewSocket(QTcpSocket *)), this, SLOT(addNewSocket(QTcpSocket *)));
    6. thread->start();
    7. }
    8. //SLOT
    9. void FServer::addNewSocket(QTcpSocket *socket)
    10. {
    11. arraySockets.push_back(socket);
    12.  
    13. socket->moveToThread(QApplication::instance()->thread());
    14. qDebug() << "FServer -> tcpSocket" << socket->thread();
    15. }
    To copy to clipboard, switch view to plain text mode 


    The code runs in a separate thread:

    Qt Code:
    1. void FThread::run()
    2. {
    3. QTcpSocket *tcpSocket = new QTcpSocket;
    4.  
    5. if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
    6. emit error(tcpSocket->error());
    7. return;
    8. }
    9.  
    10. //My code ...............
    11.  
    12. emit createNewSocket(tcpSocket);
    13.  
    14. qDebug() << "FThread -> tcpSocket" << tcpSocket->thread();
    15.  
    16. tcpSocket->waitForDisconnected();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Error:

    Qt Code:
    1. socket->moveToThread(QApplication::instance()->thread());
    To copy to clipboard, switch view to plain text mode 

    Text error:

    Qt Code:
    1. QObject::moveToThread: Current thread (0x9707250) is not the object's thread (0x970fa88).
    To copy to clipboard, switch view to plain text mode 


    Why I can not I move objects in the main thread?

    -------------------------
    I came to the conclusion that everything can be processed without the use of threads.
    Closed. Thank you.
    Last edited by virus; 22nd February 2011 at 18:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreaded server

    Looking at the documentation for the method in question: http://doc.trolltech.com/4.7/qobject.html#moveToThread. The interesting bit is this:
    Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread.

Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2010, 13:23
  2. Multithreaded Server
    By niol1000 in forum Newbie
    Replies: 1
    Last Post: 24th February 2010, 17:33
  3. Qt + gprof in a multithreaded app?
    By papercut in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 17:12
  4. Designing Multithreaded app in Qt
    By summer_of_69 in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2009, 15:46
  5. Multithreaded spend CPU 100%
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2008, 07:03

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.