Detached thread crashing

Started by Edward, December 21, 2014, 01:30:17 AM

Previous topic - Next topic

Edward

For some reason my application crashes after creating a detached thread. I created a small test to try to isolate the problem. I created a thread, set it as detached, and entered a busy loop so the thread has a chance to complete. After the busy loop ends, the application always crashes. From what I understand, detaching the thread should cause it to be cleaned up after the entry function completes. Where did I go wrong?

#include "stdio.h"
#include "stdlib.h"
#include "threads.h"

void printCode (int code, char* message) {
switch (code) {

case thrd_success:  printf("success - %s\n",message);  break;
case thrd_timedout: printf("timedout - %s\n",message); break;
case thrd_busy:     printf("busy - %s\n",message);     break;
case thrd_nomem:    printf("nomem - %s\n",message);    break;
case thrd_error:    printf("error - %s\n",message);    break;
default:            printf("? - %s\n",message);

}
}

int threadFunc (void* arg) {

printf("Hello\n");

return 0;

}

int main (int argc, char* argv[]) {

int result;

thrd_t thread;

result = thrd_create(&thread,threadFunc,NULL);
printCode(result,"creating thread");

result = thrd_detach(thread);
printCode(result,"detaching thread");

double n = 0.0;

for (int i = 0; i < 300000000; i++) {
n *= 0.01312354;
}

return 0;

}



frankie

#1
It seems to work well.
Are you using V8RC6? Previous compiler versions are broken on threads.

P.S. to create a delay is a very bad way to keep busy the CPU making it work hard. This slows the whole processes and heat the CPU. To delay use suspending functions as _sleep or Sleep.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Edward

Thanks for the reply. I switched from version 7 to 8 and still no luck. It could be that I'm on an unsupported operating system (Windows XP). I attached the project file in case the project settings might have an effect.


frankie

Edward
I tryied you project and it works on XP-32 as per attached picture.
I simply modified the include delimiters to be sure that you use the PellesC headers.
Are you sure that you are not using headers form different compilers (maybe the calling convenctions are different).
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Edward

Thank you frankie, it seems to be working now.