site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com -- Terry Hi, If i compile and run the program below with gcc -Wall -Werror -O0 -g -o pjoin pjoin.c or gcc -Wall -Werror -O0 -g -m64 -o pjoin pjoin.c However, if i compile with gcc -Wall -Werror -O0 -g -arch ppc pjoin pjoin.c the seg.fault does not occur. best regards, Stefan /***** program pjoin.c: *****/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <pthread.h> pthread_t wth, cth; static void msg(const char* s) { fprintf(stderr, "%s\n", s); fflush(stderr); } static void imsg(const char* s, int i) { fprintf(stderr, "%s %i\n", s, i); fflush(stderr); } void* worker(void* data) { while (1) { usleep(random()%100); msg("worker exit"); pthread_exit(NULL); } return NULL; } void* canceller(void* data) { usleep(random()%100); msg("canceller"); int e = pthread_cancel(wth); /* thread 2 */ if (!e) msg("cancel ok"); else imsg("cancel not ok", e); pthread_exit(NULL); return NULL; } int main() { int e = 0; int i = 0; while (1) { ++i; usleep(1000); msg("----------------------------"); fprintf(stderr," %i \n", i); pthread_create(&wth, NULL, worker, NULL); pthread_create(&cth, NULL, canceller, NULL); msg("about to join wth"); e = pthread_join(wth, NULL); imsg("join worker: ", e); msg("about to join cth"); pthread_join(cth, NULL); /* thread 1 */ imsg("joing canceller: ", e); } return 0; } _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/tlambert%40apple.com _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... It is not legal POSIX to cancel a thread which has already been joined, since its local state information has potentially been destroyed and/or freed back to the system. It's morally equivalent to freeing memory and then dereferencing the pointer that used to point to it and hoping it still lives in your address space. On Mar 25, 2009, at 4:35 PM, Stefan Solbrig wrote: I have a question regarding the pthread library. I already posted this question to the mt-smp mailing list, but it might have been the wrong list. Sorry for double-posting this question, if you are subscribed to both lists. I noticed that sometimes a pthread_cancel causes a segmentation fault if I try to cancel a thread that has been joined before. However, this behavior depends on the time passed between pthread_cancel and pthread_join. I get a segmentation fault after a couple of seconds. The gdb tells me, the seg.fault happens somewhere in ___spin_lock. I wonder if I misread the standard specifiying the pthread library, or if I ran into some other problem. The output of ` uname -pmrsv`: Darwin 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386 i386 This email sent to tlambert@apple.com This email sent to site_archiver@lists.apple.com