1

I am using C language and Linux as my programming platform.

In my user-space application. I used pthread to create a thread.

int main()
{
   pthread_t thread1, thread2;
   pthread_create( &thread1, NULL, fthread1, NULL );
   pthread_create( &thread2, NULL, fthread2, NULL );

   return 0;
}

void *fthread1( void *ptr )
{
   /* do something here */
   pthread_exit( NULL );
}

void *fthread2( void *ptr )
{
   /* do something here */  
   pthread_exit( NULL );
}

My problem is when I loop pthread_create to create again the two thread, my application memory usage is getting bigger.

while( 1 )
{
   pthread_create( &thread1, NULL, fthread1, NULL);
   pthread_create( &thread2, NULL, fthread2, NULL);
}

I determine the memory usaage using Linux ps command-line tool in VSZ column.

It seems I missed some part using pthreads API. How can I make my app don't use too much memory.

3
  • 1
    You're starting threads, each of which gets its own stack. Why would you not expect memory usage to grow?
    – Stephen
    Jun 23, 2010 at 0:12
  • But what if the thread is already finished(pthread_exit). It stack was used should be cleared?
    – domlao
    Jun 23, 2010 at 0:16
  • 1
    It's hard to know what the right answer is without knowing what you're doing and why. Certainly while(1) pthread_create is not your best option. :) The answer posted provides one possible solution.
    – Stephen
    Jun 23, 2010 at 0:23

3 Answers 3

5

You are potentially creating threads when the thread is still running/hasn't started. This is undefined behavior. (Read: very bad)

If you modify your program to do:

while( 1 )
{
   pthread_create( &thread1, NULL, fthread1, NULL);
   pthread_create( &thread2, NULL, fthread2, NULL);
   pthread_join(&thread1, NULL);
   pthread_join(&thread2, NULL);
}

you will wait for the thread to complete before starting a new thread.

Note that each thread has its own call stack and control structures, and will consume memory. It is best to limit the number of threads in an application, and to not create and destroy threads in a rapid fashion.

1
  • 2
    both our inventions jump and meet in one! (cf. The Taming of the Shrew). Jun 23, 2010 at 0:19
4

Before looping around to create new threads, make sure the present ones have finished:

while( 1 )
{
   pthread_create( &thread1, NULL, fthread1, NULL);
   pthread_create( &thread2, NULL, fthread2, NULL);
   pthread_join(&thread1);
   pthread_join(&thread2);
}

You are creating threads faster than the processor can clean them up.

0

Every thread must either be detached or joined. You don't detach your threads, you don't create them detached, and you don't join them. Your pthreads implementation has to keep the threads around forever because it has no way of knowing you never intend to join them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.