Re: Leak in pthread_create() -> pthead_cancel()
From: dtak (dtak_at_safe-mail.net)
Date: 08/11/03
- Next message: Eric Sosman: "Re: Leak in pthread_create() -> pthead_cancel()"
- Previous message: John Palmisano: "Uterm 0.9 - A Unicode / UTF-8 Terminal Emulator"
- In reply to: Eric Sosman: "Re: Leak in pthread_create() -> pthead_cancel()"
- Next in thread: Nick Albion: "Re: Leak in pthread_create() -> pthead_cancel()"
- Reply: Nick Albion: "Re: Leak in pthread_create() -> pthead_cancel()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 11 Aug 2003 14:48:18 +0159
Eric Sosman wrote:
> Nick Albion wrote:
>
>>>From the output of the program below, it seems that there might be a
>>leak in the pthreads library.
>>On each iteration, the thread "th" has a different adddress. I wonder
>>what happens after several hours?
>>
>>Is there something that I've missed out of the initialisation of the
>>thread?
>
>
> You've created them with default thread attributes,
> meaning that they're created as joinable and not as
> detached.
>
>
>>Do I have to call pthread_join() after pthread_cancel()?
>
>
> Unless they've detached themselves after starting, yes.
> Remember, pthread_cancel() doesn't cancel a thread "now,"
> it cancels it when the next cancellation point is reached.
>
>
>>Do I have to manually "free(th)"?
>
>
> Absolutely not: it's an `auto' variable, not a piece
> of dynamic memory obtained from malloc() and friends.
>
>
>>/***************************************************/
>>#include <sys/poll.h>
>>#include <pthread.h>
>>
>>void test_pthreads( void )
>>{
>> printf( "hello " );
>> poll( NULL, NULL, 1000 );
>>}
>>
>>int main( int argc, char **argv )
>>{
>> pthread_t th;
>> int i;
>>
>> for( i = 0; i < 5; i++ )
>> {
>> pthread_create( &th, NULL, (void*)&test_pthreads, NULL );
>> printf( "Thread = 0x%X\n", th );
>> poll( NULL, NULL, 100 );
>> pthread_cancel( th );
>> }
>>}
>
>
I don't know where exactly your problem is. I think this code should do
his job you want to have.
#include <sys/poll.h>
#include <pthread.h>
void test_pthreads( void )
{
printf( "hello\n" );
poll( NULL, NULL, 1000 );
}
int main( int argc, char **argv )
{
pthread_t th[5]; /* You need to build an array to register the newly
created threads */
int i;
for( i = 0; i < 5; i++ )
{
pthread_create( &th[i], NULL, (void*)&test_pthreads, NULL);
printf( "Thread = 0x%X\n", th );
poll( NULL, NULL, 100 );
}
}
- Next message: Eric Sosman: "Re: Leak in pthread_create() -> pthead_cancel()"
- Previous message: John Palmisano: "Uterm 0.9 - A Unicode / UTF-8 Terminal Emulator"
- In reply to: Eric Sosman: "Re: Leak in pthread_create() -> pthead_cancel()"
- Next in thread: Nick Albion: "Re: Leak in pthread_create() -> pthead_cancel()"
- Reply: Nick Albion: "Re: Leak in pthread_create() -> pthead_cancel()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|