This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

pthreads and atexit on cygwin


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]