NO

Author Topic: Detached thread crashing  (Read 2981 times)

Edward

  • Guest
Detached thread crashing
« on: December 21, 2014, 01:30:17 AM »
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?

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

}


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Detached thread crashing
« Reply #1 on: December 21, 2014, 02:21:58 AM »
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.
« Last Edit: December 21, 2014, 02:27:04 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Edward

  • Guest
Re: Detached thread crashing
« Reply #2 on: December 21, 2014, 03:05:53 AM »
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.


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Detached thread crashing
« Reply #3 on: December 21, 2014, 01:39:16 PM »
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

  • Guest
Re: Detached thread crashing
« Reply #4 on: December 21, 2014, 06:07:09 PM »
Thank you frankie, it seems to be working now.