LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Distributions (https://www.linuxquestions.org/questions/linux-distributions-5/)
-   -   How to increase number of threads per process? (https://www.linuxquestions.org/questions/linux-distributions-5/how-to-increase-number-of-threads-per-process-173680/)

Vaish 04-23-2004 09:28 AM

How to increase number of threads per process?
 
When I run the following code on linux it creates only 306 threads and exits.What should I do to increase threads per process?


* compile with: gcc -lpthread -o thread-limit thread-limit.c */
/* originally from:*/

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_THREADS 10000
int i;

void run(void) {
char c;
if (i < 10)
printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
sleep(60 * 60);
}

int main(int argc, char *argv[]) {
int rc = 0;
pthread_t thread[MAX_THREADS];
printf("Creating threads ...\n");
for (i = 0; i < MAX_THREADS && rc == 0; i++) {
rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
if (rc == 0) {
pthread_detach(thread[i]);
if ((i + 1) % 100 == 0)
printf("%i threads so far ...\n", i + 1);
}
else
printf("Failed with return code %i creating thread %i.\n",
rc, i + 1);
}
exit(0);
}

When i run this program i get the o/p as :

[root@ENTERPRIZE ashish]# ./thread-limit
Creating threads ...
Address of c = 3002970 KB
Address of c = 2992726 KB
Address of c = 2982482 KB
Address of c = 2972238 KB
Address of c = 2961994 KB
Address of c = 2951750 KB
Address of c = 2941506 KB
Address of c = 2931262 KB
Address of c = 2921018 KB
Address of c = 2910774 KB
100 threads so far ...
200 threads so far ...
300 threads so far ...
Failed with return code 12 creating thread 306.
[root@ENTERPRIZE ashish]#

What should I do to increase this?

Thanks & Regards
Ashish Borkar

IRIGHTI 04-24-2004 03:06 PM

There is a thread on this somewhere that said RH inherently didn't allow many threads. I think its in the programming forum.

I run Slack 9.1 and get 1533

That would be the reason *if* you run RH.

Found it: http://www.linuxquestions.org/questi...aximum+threads

Technoslave 04-24-2004 04:50 PM

Address of c = 1130590 KB
100 threads so far ...
200 threads so far ...
Failed with return code 12 creating thread 256.

Hey, at least you got a few more threads. I have a 2.6.5 kernel I compiled, however, my gcc/glib is from Fedora FC2/rpm'd in. I've tried playing with some of the things I found on the net, such as /etc/security/limits.conf and ulimit -u, but none of those work. I ran in to a problem once where I ran out of processes for something, so this has been one of those things that I've always had on the back burner to figure out.

Shade 04-28-2004 10:36 AM

I get 1533 as well on Slack 9.1

--Shade

davebwithane 03-31-2005 08:38 PM

the way to change the pthread limit is to change the
ulimit stack_size

my ulimit -s was 10240 and I got something like 306 threads.
I changed ulimit -s to 1024 now I can create 3066 threads. Nice.

I just add ulimit -s 1024 to the .bash_profile.

naveenisback 08-12-2009 08:36 AM

same problem with num of working threads
 
Hi

Im trying to run 600 threads, but only 381 threads are creating. I did ulimit -s 1024.. but still Im getting same problem.

pls suggest any other way to increase number of threads.

Mohammadi 11-23-2009 04:10 AM

How to increase number of Threads?
 
Hello,

I am also stuck at number '381'...

But I am exiting every newly created thread soon after creation.
According to me, there should not be 381 threads running at the same time as all are exited very soon.

My Code:
--------

void *receive(void *dummy)
{
pthread_t thread_idd;

while(1)
{
...
//Create a new thread to echo the data to the same host
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
}

}

void *transmit(packet *tx)
{
void *dummy;

// Transmit the received datagram to the same host
txcount = sendto( sd, (*tx).data, (*tx).count, 0, &((*tx).host),(*tx).len );
...

pthread_exit(dummy);
}
}

Mohammadi 11-23-2009 04:54 AM

Solution...
 
Hello All,

Even though we exit the thread, the threadi id is not released. Hence, if you are not waiting for the thread, use pthread_detach(tid) just after creating the thread. This will let you have as many threads as you want...

Code:
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
err = pthread_detach(thread_idd);
perror("Threaddetach ");


Cheers!





Quote:

Originally Posted by Mohammadi (Post 3766486)
Hello,

I am also stuck at number '381'...

But I am exiting every newly created thread soon after creation.
According to me, there should not be 381 threads running at the same time as all are exited very soon.

My Code:
--------

void *receive(void *dummy)
{
pthread_t thread_idd;

while(1)
{
...
//Create a new thread to echo the data to the same host
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
}

}

void *transmit(packet *tx)
{
void *dummy;

// Transmit the received datagram to the same host
txcount = sendto( sd, (*tx).data, (*tx).count, 0, &((*tx).host),(*tx).len );
...

pthread_exit(dummy);
}
}


naveenisback 02-25-2010 01:21 AM

Hi..

The problem is not actually with number of threads.. It is with the stack size.
so reduce ur stacksize per thread using

Code:

ulimit -s 1024
but be careful ur stack is not using more than 1024 bytes.
Have a good day..


All times are GMT -5. The time now is 11:22 AM.