In between midnight summer nights and dark winter days

Thread creation using pthread_create() on Leopard Thu, Nov 15 2007 08:03

After installing Leopard, I have noticed that some of the applications I have written have begun to misbehave after running for a while. It turns out that they are no longer able to create new threads, despite having plenty of free memory and only a handful of other threads running.

The culprit is a change in behaviour when calling pthread_create on Tiger vs. calling it on Leopard. On Tiger, it is possible to create millions of threads without worrying about cleaning up after them. This has changed in Leopard: Now, it is only possible to create 11002 (I wonder were that magical constant came from) threads before additional new threads can not be created. The following snippet of code demonstrates the issue:


//    A thread that just exits
void*    mythread(void *arg) { return 0; }

int main (int argc, char *argv[]) {
    int       err,
              count;
    pthread_t thread;
    while (1) {
        err = pthread_create(&thread, 0, mythread, 0);
        if (err != 0) {
            printf("Count: %d Error: %d '%s'\n", count, err, strerror(err));
            sleep(5);
        }
        count++;
        if (count % 1000 == 0)
            printf("Count: %d\n", count);
    }
    return 0;
}

To compile the code, put it in a file called pthread-test.c and compile it by running gcc -Wall -o pthread-test pthread-test.c from a terminal. Then run the binary by typing ./pthread-test . After a few seconds, the program will give the following output:

[ fourlights ] ./pthread-test
...
Count: 10000
Count: 11000
Count: 11002 Error: 35 'Resource temporarily unavailable'

The fix to this particular problem is to always explicitly detach threads that you do not intend to pthread_join() with later on. That is, once the thread has been created, it should be detached as follows:

err = pthread_create(&thread, 0, mythread, 0);
if (err != 0) {
    printf("Count: %d Error: %d '%s'\n", count, err, strerror(err));
    sleep(5);
}
pthread_detach(thread);

With this fix, the example no longer fails after the aforementioned 11002 threads.



Previous: Spaces.. Spaces.. Spaces.. Next: Spaces.. Spaces.. Spaces.. and 10.5.1

About me
My name is Daniel Stødle. I live in Tromsø, Norway, at 69.66° North. By day, I work as a researcher at the Northern Research Institute in Norway; by night, I run my company SCSC. I do most of my development on and for Mac OS X. My research is geared towards interaction with and visualization of geospatial data. Read more on my personal page.

Software

Contact me
E-mail: daniel@scsc.no

Recent posts


2011
Jul 28: Running iTunes in a debugger (gdb)
Jul 25: The /Volumes/MobileBackups directory
Jul 20: FolderGlance 3.0.1 supports Lion
Mar 03: Quick tip: Speeding up Xcode compilations
Mar 02: FolderGlance 3.0
Jan 07: Making Universal Back Button work on 10.6.5 and later

2010
Sep 03: Creating pthreads in C++ using pointers to member functions
May 31: Quickly open URLs in Terminal
May 31: Snow Leopard and automatically submitted Crash Reports
May 27: Universal Back Button released for Mac OS X
May 22: The 22 Megapixel Laptop
Feb 09: FolderGlance on MacUpdate Promo

2009
Sep 28: FolderGlance 2.5.3 is out
Sep 21: FolderGlance 2.5.1 adds features and fixes bugs
Sep 16: FolderGlance 2.5 released!
Sep 10: Intriguing: Snow Leopard ships with the iPhone's multi-touch API built-in
Sep 03: FolderGlance and Snow Leopard
Mar 15: Fixing Keynote '08 and '09 to work with the Scripting Bridge
Feb 26: A website in an image
Feb 09: Display wall multi-touch

2008
Feb 19: Spaces.. Spaces.. Spaces.. retires
Feb 08: How-to: Reverse engineering the Dock to fix Spaces
Jan 25: Interacting with wall-sized displays

2007
Dec 20: Interesting Finder bug
Dec 06: Developing applications for the iPod touch (and the iPhone)
Nov 15: Spaces.. Spaces.. Spaces.. and 10.5.1
Nov 15: Thread creation using pthread_create() on Leopard
Nov 13: Spaces.. Spaces.. Spaces..
Nov 07: FolderGlance, Leopard and the More... menu
Nov 06: FolderGlance and Screen Sieve now also on Leopard!
Sep 16: Mysterious window server hangs

Archive
2007
2008
2009
2010
2011

RSS feed
RSS

Links
SCSC
Blog frontpage