November 18, 2010

pethead pethead
Lab Rat
6 posts

1200 QThreads need create

 

this code doesn’t work on linux.
it makes
QThread::start: Thread creation error: resource temporarily unavailable

after ulimit -s 1024
GLib-ERROR **: Cannot create pipe main loop wake-up: too many files open

  1. #include <QtGui>
  2.  
  3. class MyThread : public QThread {
  4. Q_OBJECT
  5. private:
  6.     int m_nValue;
  7.  
  8. public:
  9.     MyThread() : m_nValue(1000)
  10.     {
  11.     }
  12.  
  13.     void run()
  14.     {
  15.   while (true) {
  16.    msleep(m_nValue);
  17.    //qDebug() <<  QThread::currentThreadId();
  18.    
  19.   }
  20.     }
  21. };
  22.  
  23.  
  24. int main(int argc, char** argv)
  25. {
  26.     QApplication app(argc, argv);
  27.  
  28.     MyThread     *thread;
  29.  
  30.     for (int i=0;i<1201;++i){
  31.  thread=new MyThread();
  32.     thread->start();
  33.   qDebug() << i;
  34.  }
  35.  
  36.     return app.exec();
  37. }
  38.  
  39. #include "main.moc"

8 replies

November 18, 2010

Franzk Franzk
Lab Rat
833 posts

Well, 1200 threads is overkill of course and it certainly won’t do much for your application. It’s better to use a thread pool (usually processor count or processor count + 1 threads) and handle the tasks there. Look into QtConcurrent if you just want a lot of tasks done.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

November 18, 2010

pethead pethead
Lab Rat
6 posts

in Windows that code is run good.
my app needs more than 1200 threads, 1200 just for test.

and.. above code runs on linux very well, and build 5000 threads!

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <signal.h>
  5.  
  6. #define NUM_THREADS     5000
  7.  
  8. volatile int while_condition = 1;
  9.  
  10. void sig_handler(int signal)
  11. {
  12.     while_condition = 0;
  13. }
  14.  
  15. void *do_work(void *ptr) {
  16.     size_t i = (size_t)ptr;
  17.     while (while_condition) {
  18.         sleep(1);
  19.     }
  20.     pthread_exit(NULL);
  21. }
  22.  
  23. int main(int argc, char** argv) {
  24.     pthread_t threads[NUM_THREADS];
  25.     pthread_attr_t attr;
  26.     void *ptr;
  27.     size_t i;
  28.    
  29.     signal(SIGINT, sig_handler);
  30.  
  31.     /* Initialize and set thread detached attribute */
  32.     pthread_attr_init(&attr);
  33.     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  34.  
  35.     for (i = 0; i < NUM_THREADS; ++i)
  36.     {
  37.         printf("In main: creating thread %ld\n", i);
  38.         int rc = pthread_create(&threads[i], &attr, do_work, (void *)i);
  39.         if (rc) {
  40.             printf("ERROR; return code from pthread_create() is %d\n", rc);
  41.             exit(-1);
  42.         }
  43.     }
  44.  
  45.     /* Free attribute and wait for the other threads */
  46.     pthread_attr_destroy(&attr);
  47.  
  48.     for (i = 0; i < NUM_THREADS; ++i)
  49.     {
  50.         int rc = pthread_join(threads[i], &ptr);
  51.         if (rc) {
  52.             printf("ERROR; return code from pthread_join() is %d\n", rc);
  53.             exit(-1);
  54.         }
  55.         printf("In main: stopped thread %ld\n", i);
  56.     }
  57.  
  58.     return (EXIT_SUCCESS);
  59. }

November 18, 2010

Tobias Hunger Tobias Hunger
Hobby Entomologist
3373 posts

Of course you should be able to run an arbitrary number of threads at the same time. But you can never ever run more than threads at the same time than you have processing units. It does sometimes make sense to run more than that number of threads, but only if the threads are not CPU bound (e.g. have to wait for data from the HDD).

You further need to be aware that creating and destructing threads is a somewhat expensive operation. That is the reason for systems using thread pools consisting of a fixed number of threads. These threads are then used to process jobs that get queued with the system. This allows to queue lots of jobs while keeping the thread management overhead low.

QtConcurrent is such a thread pool concept.

November 18, 2010

Volker Volker
Ant Farmer
5331 posts

See the forum thread Help With this ERROR:QThread::start: Thread creation error: Resource temporarily unavailable [developer.qt.nokia.com] for a discussion of the same problem.

There is a limit on the maximum number of threads a single application and/or a user can start.

As Tobias already mentioned, there it almost always does not make sense to start significantly more threads than you have CPU cores. In the worst case, the overhead of managing that much threads (by your app, by the OS) might be contra productive.

August 21, 2013

AlterX AlterX
Hobby Entomologist
323 posts

Linux has a default limit to 1024 max opened files…if you want to change it, just see /etc/security/limits.conf to change per user max files, or if you want a whole max files setting, take a look at
/proc/sys/fs/file-max and persists that in /etc/sysctl.conf

Bye

 Signature 

Qt Ambassador
Real-time cooperative teams: http://www.softairrealfight.net
——————————————————-
https://codereview.qt-project.org/#/c/92017

August 23, 2013

yshurik yshurik
Lab Rat
38 posts

Are you sure you need exactly “threads”? Because it is stressful for OS and your application. Maybe you need something like “stackless” approach? Or on other side you may try CSP approach (lib for example: http://www.cs.kent.ac.uk/projects/ofa/c++csp/) – that is extremely effective to bypass threads limitations.

August 26, 2013

Andre Andre
Robot Herder
6422 posts

Guys… this question is almost 3 years old now… Why pull it from the grave again?

August 28, 2013

AlterX AlterX
Hobby Entomologist
323 posts

You’re right, but I didn’t see a valid response to resolve that problem…so in the future my response can help someone else.

 Signature 

Qt Ambassador
Real-time cooperative teams: http://www.softairrealfight.net
——————————————————-
https://codereview.qt-project.org/#/c/92017

 
  ‹‹ NoSQL Databases      the stylesheet do not work in custom widget ››

You must log in to post a reply. Not a member yet? Register here!