Results 1 to 20 of 22

Thread: QThread count in Qt applications.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default

    Ok, i will change my code for one thread and will test how behaves in this case my program, after testing i will write my results here)

    What is benefit of Threaded Fortune Server Example as mentioned in Qt Examples and demos???
    Last edited by wysota; 21st October 2010 at 21:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread count in Qt applications.

    Quote Originally Posted by hashimov View Post
    What is benefit of Threaded Fortune Server Example as mentioned in Qt Examples and demos???
    Benefit over what? Over not having it that example at all? Or do you mean something different.

    Please don't reply to your own posts, edit your original posts instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QThread count in Qt applications.

    I change my code for one thread for serving all tcpsocket. And the result was no effective. because when sockets count increases the waiting packages also increases, and the bad think that it concatenates the packages which comes from the same socket, for example the socket every 1 second sends "Socket", in this case in my database inserts "SocketSocket", some times many times "SocketSocket....Socket". And CPU works in 99%. But in multi thread version CPU works 1-2%.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread count in Qt applications.

    Quote Originally Posted by hashimov View Post
    I change my code for one thread for serving all tcpsocket. And the result was no effective. because when sockets count increases the waiting packages also increases, and the bad think that it concatenates the packages which comes from the same socket, for example the socket every 1 second sends "Socket", in this case in my database inserts "SocketSocket", some times many times "SocketSocket....Socket". And CPU works in 99%. But in multi thread version CPU works 1-2%.
    My assumption is that you simply didn't do it correctly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QThread count in Qt applications.

    Qt Code:
    1. // #include "gpstcpserver.h"
    2.  
    3. #ifndef GPSTCPSERVER_H
    4. #define GPSTCPSERVER_H
    5.  
    6. #include <QtCore/QObject>
    7. #include <QtNetwork/QTcpServer>
    8. #include <QtNetwork/QTcpSocket>
    9. #include "gpstcpsocket.h"
    10.  
    11. class GpsTcpServer : public QTcpServer
    12. {
    13. public:
    14. explicit GpsTcpServer(QObject * parent = 0);
    15.  
    16. virtual void incomingConnection ( int socketDescriptor );
    17.  
    18. int _count;
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // gpstcpserver.cpp
    2.  
    3. #include "gpstcpserver.h"
    4.  
    5. GpsTcpServer::GpsTcpServer(QObject *parent)
    6. : QTcpServer(parent), _count(0)
    7. {
    8. }
    9.  
    10. void GpsTcpServer::incomingConnection(int socketDescriptor)
    11. {
    12. GpsTcpSocket * socket = new GpsTcpSocket(++_count, this);
    13.  
    14. if (!socket->setSocketDescriptor(socketDescriptor)){
    15. delete socket;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // gpstcpsocket.h
    2.  
    3. #ifndef GPSTCPSOCKET_H
    4. #define GPSTCPSOCKET_H
    5.  
    6. #include <QtCore/QObject>
    7. #include <QtNetwork/QAbstractSocket>
    8. #include <QtNetwork/QTcpSocket>
    9.  
    10. class GpsTcpSocket : public QTcpSocket
    11. {
    12. Q_OBJECT
    13. public:
    14. GpsTcpSocket(long id, QObject *parent = 0);
    15.  
    16. private:
    17. long _id;
    18.  
    19. signals:
    20.  
    21. public slots:
    22. void gpsReadReady();
    23. void gpsConnected();
    24. void gpsDisconnected();
    25. void gpsError( QAbstractSocket::SocketError socketError );
    26.  
    27. };
    28.  
    29. #endif // GPSTCPSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // gpstcpsocket.cpp
    2.  
    3. #include "gpstcpsocket.h"
    4.  
    5. #include <QtCore/QFile>
    6.  
    7. GpsTcpSocket::GpsTcpSocket(long id, QObject *parent) :
    8. QTcpSocket(parent), _id(id)
    9. {
    10. connect(this, SIGNAL(readyRead()), this, SLOT(gpsReadReady()));
    11. connect(this, SIGNAL(connected()), this, SLOT(gpsConnected()));
    12. connect(this, SIGNAL(disconnected()), this, SLOT(gpsDisconnected()));
    13. connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(gpsError(QAbstractSocket::SocketError)));
    14. }
    15.  
    16. void GpsTcpSocket::gpsReadReady()
    17. {
    18. int availableBytes = bytesAvailable();
    19.  
    20. char * data = new char[availableBytes];
    21. read(data, availableBytes);
    22.  
    23. /*QFile file("c:/data.txt");
    24.   file.open(QFile::Append);
    25.   file.write(data, availableBytes);
    26.   file.close();*/
    27.  
    28. delete [] data;
    29. }
    30.  
    31. void GpsTcpSocket::gpsConnected()
    32. {
    33. }
    34.  
    35. void GpsTcpSocket::gpsDisconnected()
    36. {
    37. }
    38.  
    39. void GpsTcpSocket::gpsError(QAbstractSocket::SocketError socketError)
    40. {
    41. Q_UNUSED(socketError);
    42. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2.  
    3. #include <QtCore/QCoreApplication>
    4. #include <QtNetwork/QHostAddress>
    5. #include "gpstcpserver.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10.  
    11. GpsTcpServer server;
    12. server.listen(QHostAddress::Any, 1000);
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd October 2010 at 13:15. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread count in Qt applications.

    Please do not inline large amount of code in your posts or at least use proper bbcode tags.

    By the way, is this code supposed to prove anything?
    Last edited by wysota; 22nd October 2010 at 13:21.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QThread count in Qt applications.

    wysota, i posted my code, what is incorrect?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread count in Qt applications.

    It's correct but it doesn't do anything. The only thing I see incorrect (but only because of knowing what "issues" you experience) is that you read all the data each and every time and that's why you get "SocketSocket" instead of two "Socket" reads. It's because you probably don't understand (or just forgot about it) that TCP is a stream of bytes and is not record-based. The fact that on the sending side you write two "Socket" words separately won't cause the receiving end to read two datagrams containing one "Socket" entry each. And you get 99% of the CPU usage probably because you are dumping the contents of the sockets to the console which is terribly time consuming.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QThread count in Qt applications.


Similar Threads

  1. Count files of a directory
    By radu_d in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2012, 03:17
  2. Replies: 0
    Last Post: 17th March 2010, 12:17
  3. Count indexes in QListView
    By been_1990 in forum Qt Programming
    Replies: 5
    Last Post: 17th December 2009, 19:21
  4. Replies: 7
    Last Post: 25th April 2009, 05:13
  5. [QGLWidget] Count FPS
    By Macok in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2009, 14:01

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.