NO

Author Topic: C11 threads, thread always crashes on exit  (Read 3570 times)

Superlokkus

  • Guest
C11 threads, thread always crashes on exit
« on: March 17, 2014, 12:19:30 PM »
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

Code: [Select]
#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);
 
}
 

Superlokkus

  • Guest
Re: C11 threads, thread always crashes on exit
« Reply #1 on: March 18, 2014, 11:22:31 AM »
I reduced the code to a more condensed version, and reproduced it with it, on another system:

Code: [Select]
#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)
« Last Edit: March 21, 2014, 11:11:38 AM by Superlokkus »

Superlokkus

  • Guest
Re: C11 threads, thread always crashes on exit
« Reply #2 on: April 15, 2014, 12:24:25 PM »
I don't want to annoy you, but a confirmation would be very nice.  :D

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: C11 threads, thread always crashes on exit
« Reply #3 on: April 15, 2014, 01:12:22 PM »
It seems fixed with PellesC V.8.00  ;)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Superlokkus

  • Guest
Re: C11 threads, thread always crashes on exit
« Reply #4 on: April 15, 2014, 10:35:18 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!