Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max number of threads Mac allows

What is the maximum number of threads macOS allows before it reports errors? I can not find an easy answer for this. I believe it is 125, but how can I find this? Thanks for any help

like image 212
whatscsce Avatar asked Jan 27 '23 07:01

whatscsce


2 Answers

As stated in my comment, this will obviously depend on the macOS version.

I don't know the exact limit, but macOS will definitely support more than 125 threads per process.

You can test this quite easily:

#import <Foundation/Foundation.h>

static NSLock * lock;

int main( void )
{
    @autoreleasepool
    {
        lock = [ NSLock new ];

        for( int i = 0; i < 10000; i++ )
        {
            [ NSThread detachNewThreadWithBlock: ^( void )
                {
                    [ lock lock ];

                    NSLog( @"Thread %i", i );

                    [ lock unlock ];

                    while( 1 )
                    {
                        [ NSThread sleepForTimeInterval: 1 ];
                    }
                }
            ];
        }

        while( 1 )
        {
            [ NSThread sleepForTimeInterval: 1 ];
        }
    }

    return 0;
}

On my MacBookPro 2018, running macOS 10.14, I can spawn more than 8000 threads.

Real number is 8188.
Since there's a main thread, plus 3 threads dedicated to dispatch queues, we might assume the limit is 8192, which totally makes sense.

But still, this might depend on the hardware, number of CPUs/Cores.

As a side-note, if you are concerned about the maximum number of threads, then you shouldn't use threads.

Instead, use dispatch queues provided by Grand Central Dispatch (CGD):

dispatch_async
(
    dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0 ),
    ^( void )
    {
        /* ... */
    }
);

You can obviously create your own serial or concurrent queues:

dispatch_queue_create( "MyQueue", DISPATCH_QUEUE_CONCURRENT );

On concurrent dispatch queues, this will let macOS manage the available resources, spawning new threads when it's OK to do so.

This is the way you should use concurrency. Try to forget about threads, and simply start using GCD.

like image 61
Macmade Avatar answered Feb 05 '23 17:02

Macmade


Only 2048 threads per process. More attempts cause code 35 in pthread_create: enter image description here

The thread limit can be increased to 5000 by activating the macOS server performance mode which was originally meant to be used with macOS Server machines.

To active, go to the Terminal app to enter

sudo nvram boot-args="serverperfmode=1 $(nvram boot-args 2>/dev/null | cut -f 2-)

and restart the computer.

To check if performance mode is active:

nvram boot-args

To deactivate:

sudo nvram boot-args="$(nvram boot-args 2>/dev/null | sed -e $'s/boot-args\t//;s/serverperfmode=1//')"

See also Turn on performance mode for macOS Server

like image 33
Sergei Krivonos Avatar answered Feb 05 '23 19:02

Sergei Krivonos