delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/08/29/01:22:43

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <20030829052256.21826.qmail@linuxmail.org>
MIME-Version: 1.0
From: "peter garrone" <pgarrone AT linuxmail DOT org>
To: "Thomas Pfaff" <tpfaff AT gmx DOT net>
Cc: cygwin AT cygwin DOT com
Date: Fri, 29 Aug 2003 13:22:56 +0800
Subject: pthreads and atexit on cygwin
X-Originating-Ip: 192.10.200.223
X-Originating-Server: ws5-6.us4.outblaze.com

Hi,
  I have a noticed a difference in execution between cygwin and linux for pthreads and atexit. If exit is called by a thread, then functions lodged with atexit by the main thread do not get invoked on cygwin, but do get invoked on linux. Because I am unsure if this difference has been noted by anyone, I am describing it.

My test function is at the end of this message.
Looking at glibc, exit and atexit appear to use a single global pointer (__exit_funcs in stdlib/exit.c, cxa_atexit.c) whereas the cygwin newlib code (newlib/libc/stdlib/atexit.c) uses a pthread-keyed datum (macro _REENT) returned by __getreent(), different for each thread.

While I am no expert on this, logically I prefer the glibc approach,
because if I want something to happen at the end of a thread and to exit a thread I call pthread_exit and pthread_cleanup_push,
while if I want something to happen when the whole process finishes, I call atexit and I dont want to worry about what thread
does the terminating.


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

static pthread_cond_t started = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t a_thread;

static void exitfunc(void)
{
	printf("exit function invoked.\n");
}

static void * thread_routine(void * data)
{
	exit(0);
}

int main(int argc, int argv)
{
	int r;
	r = atexit(exitfunc);
	assert(r == 0);

	if(argc > 1)return 0;

	r = pthread_mutex_lock(&a_mutex);
	assert(r == 0);

	r = pthread_create(&a_thread, NULL, thread_routine, NULL);
	assert(r == 0);

	r = pthread_cond_wait(&started, &a_mutex);
	assert(r == 0);

	return 0;
}

-- 
______________________________________________
http://www.linuxmail.org/
Now with e-mail forwarding for only US$5.95/yr

Powered by Outblaze

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019