Me and some other person on stack overflow faced a problem with pelles C C11 threads support: Our threads are always crashing (access violation) on thrd_exit(). On this example it's a detached thread but it also crashes with a non detached thread on join, or even when main() is sleeping. Please also read our stackover flow posts: http://stackoverflow.com/questions/21787244/bug-in-implementation-of-thrd-detach-of-desired-behaviour/22452824#22452824 (http://stackoverflow.com/questions/21787244/bug-in-implementation-of-thrd-detach-of-desired-behaviour/22452824#22452824)
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <strings.h>
#include <time.h>
struct foo
{
int i;
char * bar;
};
int thread_func(void *arg)
{
printf("Entering 2nd thread\n");
struct timespec sleeptime;
sleeptime.tv_sec = 5;
thrd_sleep(&sleeptime,NULL);
printf("Second thread finished sleeping\n");
//struct foo *sn;
//sn = (struct foo *) arg;
//printf("%d %s\n",sn->i,sn->bar);
thrd_exit(thrd_success);
}
int main (int argc, char *argv[])
{
struct foo *foo;
foo = malloc(sizeof(struct foo));
foo->i = 5;
char *bar = malloc(sizeof(char)*10);
strcpy(bar,"SNAFU");
foo->bar = bar;
thrd_t secondthread;
if (thrd_create (&secondthread,thread_func,foo) != thrd_success )
{
perror("Thread failed");
return 1;
}
thrd_detach(secondthread);
printf("Main goes on\n");
struct timespec sleeptime;
sleeptime.tv_sec = 10;
thrd_sleep(&sleeptime,NULL);
printf("Main finished sleeping\n");
thrd_exit(thrd_success);
}
I reduced the code to a more condensed version, and reproduced it with it, on another system:
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <strings.h>
#include <time.h>
int thread_func(void *arg)
{
printf("Entering 2nd thread\n");
struct timespec sleeptime1;
sleeptime1.tv_sec = 5;
thrd_sleep(&sleeptime1,NULL);
printf("Second thread finished sleeping\n");
thrd_exit(thrd_success);
}
int main (int argc, char *argv[])
{
thrd_t secondthread;
if (thrd_create (&secondthread,thread_func,NULL) != thrd_success )
{
perror("Thread failed");
return 1;
}
thrd_detach(secondthread);
printf("Main goes on\n");
struct timespec sleeptime2;
sleeptime2.tv_sec = 10;
thrd_sleep(&sleeptime2,NULL);
printf("Main finished sleeping\n");
thrd_exit(thrd_success);
}
BTW: I am using Pelles C 7.00.355 (Win64) on Win7 64 bit Service Pack 1 (7601)
I don't want to annoy you, but a confirmation would be very nice. :D
It seems fixed with PellesC V.8.00 ;)
Quote from: frankie on April 15, 2014, 01:12:22 PM
It seems fixed with PellesC V.8.00 ;)
Indeed, it is fixed. Thanks to all who are working on pelles c, always nice to have an useable and free c compiler on C!