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 QThread count in Qt applications.

    Hi. For testing i create 1000 threads in my application with QThread class, and all this threads are running. But i get QThread::start: Failed to create thread (The access code is invalid.) this message. What is problem?

    my class is.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "mythread.h"
    3.  
    4. ......int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. QCoreApplication * app = qApp;
    9.  
    10. for (int i=0; i < 1000; i++)
    11. {
    12. MyThread * thread = new MyThread(i, app);
    13. MyThread::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    14. MyThread::connect(thread, SIGNAL(terminated()), thread, SLOT(deleteLater()));
    15. thread->start();
    16. }
    17.  
    18. return a.exec();
    19. }
    20.  
    21. #ifndef MYTHREAD_H
    22. #define MYTHREAD_H
    23. #include <QtCore/QThread>
    24. class MyThread : public QThread
    25. {
    26. Q_OBJECT
    27. public:
    28. explicit MyThread(int number, QObject *parent = 0);
    29. void run();
    30. private:
    31. int number;
    32. signals:
    33. public slots:
    34. };
    35. #endif // MYTHREAD_H
    36. #include "mythread.h"
    37. #include <stdio.h>
    38.  
    39. MyThread::MyThread(int number, QObject *parent) :
    40. QThread(parent), number(number)
    41. {
    42. }
    43.  
    44. void MyThread::run()
    45. {
    46. while (true)
    47. {
    48. //printf("Running thread number: %d\n", number);
    49. QThread::msleep(10000);
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st October 2010 at 20:28.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QThread count in Qt applications.

    Every thread consumes resources. Just as one example, each thread has a stack associated with it. A typical stack size might be a megabyte, although this varies considerably from machine to machine. A thousand threads, then, would consume a gigabyte of memory in stack space alone, along with whatever other memory is required by stack overhead.

    Everything on a computer has limits. If any were infinite, they would have trouble fitting it all in that little box.

  3. The following user says thank you to SixDegrees for this useful post:

    hashimov (21st October 2010)

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

    Thank you, for your answer. Can you help me how solve this problem, if it resolvable?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.

    Do you really need 1000 threads? Do you have 1000 cores in your computer?
    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.


  6. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QThread count in Qt applications.

    On some systems (Linux) you can adjust the per-thread stack size. I don't know if this is possible on Windows; probably.

    The real question is: what do you need 1000 threads for? Unless you have 1000 processors, it's unlikely there's any good reason to spawn so many. You're probably better off using a thread pool that reuses a fixed (small) number of threads; Qt provides a QThreadPool class for this purpose, although I've never used it.

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

    Actually i need this count of threads for listening QTcpSocket. My program listens some devices which sends some tcp packeges to my server, and my server creates threads for every device, such as Fortune Server Example in qt examles. and count of this devices may by 10000 and more.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.

    In that case don't create one thread per device, there is no need to do that, you can handle all clients in one thread.
    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. #8
    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.

    No. in case of one thread there may be some waitings, an other way every my thread also commonicates with QTcpSocket individually. I need per thread for per device.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.

    No, you don't need one thread per device. If you have 1000 threads and one CPU then you will wait much much much much much longer than when handling all connections in one thread. This is a common legend that you need one thread per connection to be able to handle multiple clients and here we fight very hard to counter this riddiculous belief.
    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.


  11. #10
    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.

    Ok, assume that all connections are one threads. Then in this case 999 QTcpSocket-s must wait in some loop while one QTcpSocket is reading data from socket? Take into consideration that every device send the packages every 1 second or 0.1 second. Would be there pending packeges for reading?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
    Ok, assume that all connections are one threads. Then in this case 999 QTcpSocket-s must wait in some loop while one QTcpSocket is reading data from socket?
    No loop. If you have 1000 threads and one CPU then 999 sockets are waiting anyway. And the CPU has to waste its cycles to context switch between those 1000 threads.

    Take into consideration that every device send the packages every 1 second or 0.1 second.
    A lot can happen within 0.1 seconds. Especially that not all devices send data at the same time. This would result in network conflicts anyway.

    Would be there pending packeges for reading?
    I don't know how large each block of your data is but assuming it is somewhere between 100 bytes and 1 kilobyte per 0.1 second then one thread will easily handle many such connections. If it can handle 1000 - I don't know, probably yes but if you have two CPUs and possibly more than one network card then it might be wise to distribute the traffic across two network cards and two threads. With 1000 threads you're just wasting resources.
    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.


  13. The following user says thank you to wysota for this useful post:

    hashimov (21st October 2010)

  14. #12
    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.

  15. #13
    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.

    My program works fine for probabalu 700 threads. But when count increases program gets QThread::start: Failed to create thread (The access code is invalid.) message.

  16. #14
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    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???
    It's meant as an example. Not a particularly good one when it comes to real-world implementation.

    The traditional way of handling socket connections uses a single process that listens on the socket and forks() itself as needed. Threads can handle this using a lighter weight framework, but they still come at a price of stack space and other overhead.

    Use a thread pool that recycles a fixed number of threads. If you think you need more, it's easy to increase the number of threads, but as others have already pointed out you're going to be much more limited by network conflicts and CPU contention anyway; "waiting" for a thread to become available is simply a reflection of other hardware realities that can't be overcome. Your hardware can't physically handle 1000 simultaneous connections - it can only handle one at a time.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.