开发者

Thread Synchronization - Maximum number of threads for a process

I was trying out a simple program with regard to thread synchronization using pthreads on a linux syst开发者_JS百科em. My program's source can be found at http://pastebin.com/5EWS68NL.

The program essentially has to print the number 1 to LIMIT (an arbitary number) in increasing order. A thread is assigned to print each number, it waits till it's condition variable is signaled, and it prints the number.

The program works fine till LIMIT=380. More than that, the program hangs. That is, I don't think pthread_create() returns an error since I've tried to print the error number and also included fflush(stdout) in case of an error.

The maximum number of threads for the system can be specified in /proc/sys/kernel/threads-max. I did try to increase it an arbitarily large number, with no success. The program plainly hangs if the number of threads is about 380.

Any thoughts why this could be the case? Will this value '380' differ in different systems?

Thanks and Regards,

Hrishikesh Murali


I changed LIMIT to be 50000 and it errors out on lack of memory, which is what I expected, after around 32,000 threads. Otherwise it works fine.

I made this little change to stop execution on pthread_create error.

    if ( ret_val )
    {
        printf("Couldn't create thread number %d - %s\n", iter, strerror(ret_val) );
        fflush(stdout);
        exit(1);
    }

Since you don't stop on an error maybe you just fell through and started printing numbers and didn't see your error message?

Anyway I assume the error will apply to your machine. Threads do use memory and memory isn't infinite although 380 does seem a little low. Maybe the thread stack size is set high on your machine?

Since this is a toy program change it to start printing after pthread_create errors out and then loop back and continue creating threads again. You should pthread_join your threads or create them as detached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜