Problems with threads limit.

P

pavvka

I'm making multithread server on Linux. Everything works fine, but
server reaches threads limint and cannot create any one more. I've
wrote simple test code to test this problem.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

using namespace std;


pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
pthread_mutex_lock( &mutex1 );
counter++;
int nr = counter;
printf("Thread ID: %d \n", nr);
pthread_mutex_unlock( &mutex1 );
}

main()
{
char *message1 = "Thread";
int iret1;

for (int i = 0; i < 400; i++)
{
pthread_t *thread1 = new pthread_t;

if (iret1 = pthread_create(thread1, NULL,
print_message_function, (void *) message1))
{
printf("Thread creation failed: %d\n", iret1);
}
}

exit(0);
}

Got results like this:

pawka@pawka-desktop:~/Darbai$ g++ -lpthread test.cpp -o test
pawka@pawka-desktop:~/Darbai$ ./test
Thread ID: 1
Thread ID: 2
Thread ID: 3
<...>
Thread ID: 378
Thread ID: 379
Thread ID: 380
Thread creation failed: 12
Thread creation failed: 12
Thread creation failed: 12
<...>

Every time around 380 thread my program fails creating threads. How fix
it?
 
P

peter koch

I'm making multithread server on Linux. Everything works fine, but
server reaches threads limint and cannot create any one more. I've
wrote simple test code to test this problem.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

using namespace std;

[snip]

Every time around 380 thread my program fails creating threads. How fix
it?

You need to redesign your application. 380 threads is just plain silly
unless you have at least 100 physical processors.

/Peter
 
A

Alan Johnson

I'm making multithread server on Linux. Everything works fine, but
server reaches threads limint and cannot create any one more. I've
wrote simple test code to test this problem.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

using namespace std;


pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
pthread_mutex_lock( &mutex1 );
counter++;
int nr = counter;
printf("Thread ID: %d \n", nr);
pthread_mutex_unlock( &mutex1 );
}

main()
{
char *message1 = "Thread";
int iret1;

for (int i = 0; i < 400; i++)
{
pthread_t *thread1 = new pthread_t;

if (iret1 = pthread_create(thread1, NULL,
print_message_function, (void *) message1))
{
printf("Thread creation failed: %d\n", iret1);
}
}

exit(0);
}

Got results like this:

pawka@pawka-desktop:~/Darbai$ g++ -lpthread test.cpp -o test
pawka@pawka-desktop:~/Darbai$ ./test
Thread ID: 1
Thread ID: 2
Thread ID: 3
<...>
Thread ID: 378
Thread ID: 379
Thread ID: 380
Thread creation failed: 12
Thread creation failed: 12
Thread creation failed: 12
<...>

Every time around 380 thread my program fails creating threads. How fix
it?

Your question is off-topic for comp.lang.c++, follow-ups set to
comp.os.linux.development.

As for your question, I think probably you need to look up the man page
for pthread_join.
 
I

Ian Collins

Every time around 380 thread my program fails creating threads. How fix
it?
Linux doesn't run on anything big enough to justify 380 threads!
Rethink your design and ask on a Linux group when you do.
 
R

Rolf Magnus

Ian said:
Linux doesn't run on anything big enough to justify 380 threads!

Well... it runs on mainframes with thousands of CPUs. But I doubt the OP is
using one of those.
 
K

Kaz Kylheku

I'm making multithread server on Linux. Everything works fine, but
server reaches threads limint and cannot create any one more. I've
wrote simple test code to test this problem.

Your sample code shows nothing because you are creating joinable
threads and not calling pthread_join on them. A joinable thread
continues to occupy resources until it is joined. Those resources may
stand in the way of being able to create threads.

A reasonable server only has enough threads to keep all of the CPU's
busy. Usually, the number of threads to achieve that is some small
number, greater than one, multiplied by the number of available CPU's.
In a very compute-intensive application, that multiple tends to be
smaller. If there is a lot of blocking I/O, the factor is larger.
 
I

Ian Collins

Rolf said:
Ian Collins wrote:




Well... it runs on mainframes with thousands of CPUs. But I doubt the OP is
using one of those.
With a single kernel instance?
 
V

VL

Ian said:
With a single kernel instance?
I did not hear of "thousands" of CPUs, but 1024 CPUs is possible, I think:
http://www.sgi.com/company_info/newsroom/press_releases/2006/june/altix4700.html

Quoted from the PR:
"On its powerful and acclaimed SGI® Altix® 4700 blade platform and a beta
version of SUSE® Linux Enterprise Server 10 from Novell, SGI demonstrated a
single system image (SSI) running on a world-record 1,024 processors."

I do not know if "single system image" means the same as your term "single
kernel instance", but it sounds likely to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top