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?
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?