[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: pthreads help needed to diagnose #98866



On Mon, May 28, 2001 at 07:03:00PM -0500, Gordon Sadler wrote:

> Hmm, I still seem to get differing behavior. BROKEN and WORKS2 produce
> same/similar output, WORKS1 continues above results (I increased sleep
> to 10, no difference).
> 
> I have made a change to get consistent behavior. Apparently the reason
> WORKS1 was differing was, it needed a global Semaphore..
> I'm not positive if you need to pass the Semaphore to thread->Run(), but
> it now produces the same ouput for all three defines, with no massive
> increase in VSZ.

In my application, I'm probably going to use semaphores to get around this
issue, but I don't understand why it works with static linking and not with
dynamic linking.  I suppose it could even be a difference between the static
and shared libpthreads, but so far I have been unable to reproduce this problem
using the pthread API directly.  The attached program works fine, with no
memory leak, doing essentially the same pthread calls, regardless of how it is
linked.  It is a modified version of a test program written by Nick Hynes
<nick@1site.co.nz>, which I found when doing a web search for 'pthread memory
leak' or some such.

-- 
 - mdz
#include <iostream.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void* jump_start( void *null )
{
  //sleep( 10 );

  pthread_exit( (void*)6 );
  return (void*)6;
}

int main(int argc, char *argv[])
{
    // Create the pthread attribute...
    pthread_attr_t thread_attr;

    pthread_attr_init( &thread_attr );
    pthread_attr_setdetachstate( &thread_attr, PTHREAD_CREATE_JOINABLE );
    
    pthread_attr_setinheritsched(&thread_attr, PTHREAD_INHERIT_SCHED);

    sleep( 4 );

    cout << "Creating threads..." << endl;
    for ( int i=0; i<512; i++ )
    {
        int error_code;
        pthread_t wait_tid;

        // Initialize this variable before we open the socket...
        error_code = pthread_create( &wait_tid, &thread_attr, jump_start,
&wait_tid );
	pthread_detach( wait_tid );

        if ( error_code )
        {
            cout << "Couldn't create the thread..." << endl;
        }
        else
       {
            cout << wait_tid << ", " << endl;
       }
    }

    // Destroy the attributes..
    pthread_attr_destroy( &thread_attr );

    cout << "Created all threads... sleeping(6)..." << endl;
    sleep( 6 );

    return EXIT_SUCCESS;
}


Reply to: