seg.fault in pthread library
seg.fault in pthread library
- Subject: seg.fault in pthread library
- From: Stefan Solbrig <email@hidden>
- Date: Thu, 26 Mar 2009 00:35:48 +0100
Hi,
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.
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
I get a segmentation fault after a couple of seconds. The gdb tells
me, the seg.fault happens somewhere in ___spin_lock.
However, if i compile with
gcc -Wall -Werror -O0 -g -arch ppc pjoin pjoin.c
the seg.fault does not occur.
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
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()0);
msg("worker exit");
pthread_exit(NULL);
}
return NULL;
}
void* canceller(void* data)
{
usleep(random()0);
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 (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden