🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.os.linux.programowanie
Article #72356

"destruktor" pthread_key_create nie jest wołany dla głównego wątku

#72356
From: macu
Date: Tue, 04 Oct 2011 21:33
68 lines
2743 bytes
Witam,

dziś naciąłem się na kod który nie zwalnia wszystkich allokacji
a używa TLS z biblioteki pthread.

W wywołaniu pthread_key_create() drugi argument jest funkcją 
/destructor/ wołaną po zakończeniu wątku gdy ten zawołał w swoim 
kontekście pthread_setspecific().

Niestety okazuje się, że destruktor jest co prawda wywołany
dla wątków, natomiast nie jest wołany dla głównego wątku programu.

Z palca napisałem poniższy kod, więc nie mam pewności czy się
skompiluje od razu poprawnie

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

static pthread_key_t key = -1;

void clean_thr_res( void* par )
{
     printf( "%s ptr 0x%X\n", __FUNCTION__, ptr );
     free( par );
}

int thread( void* par )
{
     void* ptr = malloc( 10 );
     printf( "%s ptr 0x%X\n", __FUNCTION__, ptr );
     pthread_setspecific( key, ptr );
     return 0;
}

int main()
{
     pthread_t thr;
     // Allokacja i przechowanie w TLS głównego thread'a.
     void* ptr = malloc( 10 );
     printf( "%s ptr 0x%X\n", __FUNCTION__, ptr );
     pthread_key_create( &key, clean_thr_res ); // Inicjuj TLS
     pthread_setspecific( key, ptr ); // Przechowaj zasob w TLS

     // Testuj z dodatkowego thread'a.
     pthread_create( &thr, 0, thread, 0 )
     pthread_join( thr, 0 );

     return 0;
}


No więc dla powyższego kodu output będzie następujący:
main ptr 0x1234
thread ptr 0x2233
clean_thr_res 0x2233

i niestety allokacja 0x1234 nie bedzie zwolniona.
Nie wygooglałem jawnej informacji że dla głównego wątku /który
pewnie nie jest utworzony pthread_create()/ funkcja podana
w pthread_key_create() nie jest wołana.

Pytanie: czy wiedzieliście o tym zachowaniu pthread i macie jakieś 
namiary na informacje opisujące ten problem?
A może znacie jakieś rozwiązanie na to?

Pozdrawiam
Mariusz
Technical Details
Message-ID: <j6fu63$ink$1@news.onet.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!.POSTED!not-for-mail
From: macu <macu@polbox.pl>
Newsgroups: pl.comp.os.linux.programowanie
Subject: "destruktor" pthread_key_create nie jest wołany dla
 głównego wątku
Date: Tue, 04 Oct 2011 23:33:43 +0200
Organization: http://onet.pl
Lines: 68
Message-ID: <j6fu63$ink$1@news.onet.pl>
NNTP-Posting-Host: 178.254.101.155
Mime-Version: 1.0
X-Trace: news.onet.pl 1317764099 19188 178.254.101.155 (4 Oct 2011 21:34:59 GMT)
X-Complaints-To: niusy@onet.pl
NNTP-Posting-Date: Tue, 4 Oct 2011 21:34:59 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-Accept-Language: en-us, en
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit