Basic GUI and odd thread error

This is a discussion on Basic GUI and odd thread error within the C++ Programming forums, part of the General Programming Boards category; I want to do a simple gui for an otherwise unglamorous program, but I want to use only code that's ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Basic GUI and odd thread error

    I want to do a simple gui for an otherwise unglamorous program, but I want to use only code that's already included with C++ (I'm using Fedora with KDE. Someone mentioned QT already, but I couldn't find a good command reference for it). "Back in the day" I would use ascii to represent windows (you remember those ancient DOS utilities right?). That would probably work too if I could only remember the commands for outputting characters to the screen.

    Anyway, problem 2. I've threaded a function call using pthread_create and all is well. It execute flawlessly 303 times then gets stuck. I've verified that the loop that the thread call is in continues to function, but the counter I'm using to restrict the number of threads gets stuck which suggests that after a while, the threads are not exiting the function. I don't have the actual code on this computer, but I'll try to approximate here:

    Code:
    #define THREADS_TO_LAUNCH=1000;
    #define MAX_THREADS=20;
    int numthreads=0;
    int thread_count=0;
    
    void *threaded(void *nothing)
    {
      ...
      do some stuff
      ...
    
      // As the thread exits, the last thing I want to do is decrement 
      // the number of active threads and increment the number of 
      // total threads launched
    
      numthreads--;
      thread_count++;
    }
    
    int main()
    {
      while (threads_count < THREADS_TO_LAUNCH)
      {
         if (numthreads < MAX_THREADS)
         {
           numthreads++;
           pthread_create(NULL,NULL,threaded,NULL);
         }
      }
    
    }
    Anyway, the code all works splendidly, but hangs after 303 launches every time. The main loop never ends because the threads stop exiting or stop decrementing the numthreads variable (I can't tell which). Either way, the loop goes on endlessly. Any ideas? This is a very strange error that I'm sure someone else could see instantly, but I just can't.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Perhaps
    Code:
    volatile int numthreads=0;
    volatile int thread_count=0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Additional info:

    I did some more testing and found out that after the 303rd thread call, the pthread_create function returns an error code of 12. I realize this may be a newb question, but how do I find out what that actually means? The man pages show EAGAIN, EPERM, and EATTR, but those aren't defined so how do I test them?

    Even then, why would any of those trigger after 303 threads? I'm restricting the maximum number at a time to 20 so it shouldn't be hitting a thread limit.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by Salem
    Perhaps
    Code:
    volatile int numthreads=0;
    volatile int thread_count=0;

    Good thinking. I tried it, but unfortunately, it behaves exactly the same way.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Additional info 2:

    I found a link on google (somehow) that lists error code 12 as being a ENOMEM error (would be nice if that were documented :/).

    Assuming that's the case, why am I getting this error? The two classess defined in the the function I'm calling are both properly deleted at the end of the function and exiting the called function should delete the thread automatically.

    Is this a simple memory leak?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > but those aren't defined so how do I test them?
    Errors are listed in errno.h

    Also
    http://unixhelp.ed.ac.uk/CGI/man-cgi?pthread_join+3
    http://unixhelp.ed.ac.uk/CGI/man-cgi?pthread_exit+3
    I think you need a more organised exit of the thread rather than just falling off the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by Salem
    > but those aren't defined so how do I test them?
    Errors are listed in errno.h

    Also
    http://unixhelp.ed.ac.uk/CGI/man-cgi?pthread_join+3
    http://unixhelp.ed.ac.uk/CGI/man-cgi?pthread_exit+3
    I think you need a more organised exit of the thread rather than just falling off the end.
    So are you suggesting an explicit call to pthread_exit() at the end of the function? I tried that and it didn't make any difference though...

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    China
    Posts
    1
    You may add
    Code:
    pthread_detach(pthread_self());
    to the body of your thread function.
    Last edited by m2kcoder; 09-21-2006 at 09:32 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Thanks for the help. I managed to fix it by changing it to a for loop that just instantiates the full MAX_THREADS number of threads from the beginning. I then moved the while loop into the main function of the thread so each thread continues to operate until a global variable signals that it's time to stop. It works basically the same way, but I don't run out of memory and I lose the overhead from creating and destroying threads constantly.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21