LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2006, 09:28 PM   #1
goodman888
LQ Newbie
 
Registered: Sep 2006
Location: HK
Posts: 3

Rep: Reputation: 0
pthread_create Cannot allocate memory error code 12


every time I run the following code, it will stop at i=303, i.e. only 303 threads can be created.
I searched Google with my best and couldn't find the answer. There were similar cases reported but no answers....

hope someone can help...

Code:
struct Threadargs {
    int seq;
};

void getCurrentDateTime(char *sdate)
{
    time_t tnum;
    struct tm *tmdate;

    time(&tnum);
    tmdate = localtime(&tnum);

    strftime(sdate, 21, "%Y-%m-%d %H:%M:%S", tmdate);	
}

unsigned int genRandom(int digit=5)
{
	double min, max;

	if(digit<1)		return 0;

	digit--;
	min = pow(10.0, digit);
	max = pow(10.0, digit+1) - 1;
	double range = max-min+1;

	return (int)min + int( range * rand() / (RAND_MAX+1.0) );
}

void *writefile(void *threadArgs)
{
	char tempStr[128]="", now[128] = "";
	FILE *fp = fopen("test.txt", "ab+");
	
	int seq = ((struct Threadargs *) threadArgs)->seq;

	int error = pthread_detach(pthread_self());
	if(error) {
		printf("pthread_detach: %d %s\n", error, strerror(error));
		pthread_exit(0);
	}

	unsigned int wait = genRandom(1);
	usleep(wait * 10000);

	if(fp) {
		getCurrentDateTime(now);
		sprintf(tempStr, "%s Thread No: %d Thread ID: %d\n", now, seq, pthread_self());
		fputs(tempStr, fp);
		fclose(fp);
	} else {
		printf("Critical error. Failed to write text.txt\n");
		exit(0);
	}
	
	free(threadArgs);
	pthread_exit(0);
}


int main(int argv, char *argc[])
{
	srand( (unsigned)time(0) );		// seeding random number

	pthread_t th_id=0;

	for(int i=0; i<310; i++)
	{
		Threadargs *t;		
		t = (Threadargs *) malloc ( sizeof(Threadargs) );	
		memset(t, 0, sizeof(Threadargs));
		t->seq = i;
		int error = pthread_create(&th_id, NULL, &writefile, t);
		if(error) {
			printf("pthread_create error code at %d: %d %s\n", i, error, strerror(error));
			break;
		}
	}
	sleep(2);
	return 1;
}
 
Old 09-22-2006, 09:52 PM   #2
goodman888
LQ Newbie
 
Registered: Sep 2006
Location: HK
Posts: 3

Original Poster
Rep: Reputation: 0
Cool

Oh, I just found the solution.

There are just too many threads writing the same file. The solution is to add

Code:
usleep(1);
int error = pthread_create(&th_id, NULL, &writefile, t);
just force the main function to sleep even just 1us before creating another thread....

also, on my machine, i found around 500 writes can be performed in thread mode per second.
but if I don't use thread (change writefile() back to normal function), more than 15000 writes can be performed in 1 second!!

The overhead of creating a thread is quite large... so should use only with reasons.
 
Old 04-14-2008, 04:20 AM   #3
UlrichWinkler
LQ Newbie
 
Registered: Apr 2008
Posts: 1

Rep: Reputation: 0
Set the PTHREAD_CREATE_DETACHED flag

I had the same problem, the solution is to use the PTHREAD_CREATE_DETACHED flag.

Here is my test (quick and dirty; i'm using the ACE lib)

Code:
#define USE_ACE 0
void* theFunction ( void* )
{
  return 0;
}

void maxThreadTest ()
{
  const int MAX = 1000000;

  ACE_Time_Value start = ACE_OS::gettimeofday();

  for ( int i = 0; i < MAX; i++ )
 {
    int ret;

    if ( ! ( i % (MAX / 100) ) ) std::cerr << "..." << i  ;

#if USE_ACE
    ACE_thread_t id;
    ACE_hthread_t ha;
#else
    pthread_t thread;
    int tid = 0;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
#endif


#if USE_ACE
    ret = ACE_Thread::spawn_n ( &id, 1 , (ACE_THR_FUNC ) theFunction, 0, THR_NEW_LWP  | THR_DETACHED , ACE_DEFAULT_THREAD_PRIORITY  , 0 , 0, &ha);
# else
    ret = pthread_create(&thread, &attr , theFunction, (void *)tid);
#endif


#if USE_ACE
    if ( ret <= 0 )
#else
   if ( ret )
#endif
    {
      std::cerr << "Threads created " << i << std::endl ;
      perror ( "pthread_create" );
      exit ( 1 );
    }
  }
  ACE_Time_Value stop = ACE_OS::gettimeofday();

  std::cerr << std::endl;
  int delta  = stop.msec() - start.msec ();
  std::cerr << "Create " << MAX << " threads in " << delta / 1000.0
            << "s. (" <<  ( (double)MAX /  ( (double) delta / 1000 ) ) << " threads/s)" << std::endl;

  std::cout << "exit (succes)." << std::endl;
}
 
Old 04-14-2008, 11:34 PM   #4
goodman888
LQ Newbie
 
Registered: Sep 2006
Location: HK
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks a lot. Let me try!!
 
Old 05-20-2008, 07:19 PM   #5
egag
Senior Member
 
Registered: Jul 2004
Location: Netherlands
Distribution: Slackware
Posts: 2,721

Rep: Reputation: 52
error "12" from pthread_create--- " I had the same problem, the solution is to use the PTHREAD_CREATE_DETACHED flag."

Thanks a lot, Ullrich
You've made me very happy...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error inserting lockd: Cannot allocate memory - Linux 2.6.13.2 Iceberg69 Linux - Software 1 09-29-2005 10:20 PM
Cannot allocate memory Terroth Linux - General 13 02-09-2005 05:22 AM
allocate more memory? sockknitter Debian 3 11-06-2004 05:23 PM
Error: cannot allocate memory ion111 Linux - Software 7 07-04-2004 06:33 AM
unable to allocate memory vedant Linux - Hardware 0 07-15-2003 07:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration