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;
}