NO

Author Topic: Bug in implementation of thrd_detach(), of desired behaviour?  (Read 3570 times)

neo313

  • Guest
Bug in implementation of thrd_detach(), of desired behaviour?
« on: February 14, 2014, 07:38:41 PM »
I will be discussing the function thrd_detach(), from the threads.h header of the new c11, which is also included in the latest pelles ide.

( the thread I wil refer below will always be the thread created, not the main thread; the main never exists in examples below )
The only information I can find on the function thrd_detach() is from the pelles help and this: http://en.cppreference.com/w/c/thread/thrd_detach. Both basically say the same thing( please read them if you haven't ).
I interpret the description offered in those documents that the function can detach the thread before the thread actually exits. Furthermore it states that the function resources will be freed automatically when the thread exits, implying that thrd_detach() can detach a thread before the thread exited.
That is the problem; thrd_detach() works fine when the thread in question already exited. But if the thread was still running when thrd_detach() was called, it causes an exception violation when the thread exists, ( not when you call thrd_detach() )

Is that the desired behavior? ( if it is, it goes against the logic of the description, in my opinion )

Here is the example which is described in words above.

Code: [Select]
#include <threads.h>
#include <assert.h>
#include <stdio.h>

//#define THREAD_EXITS_BEFORE_DETACH_CALL //doesn't crash if this is defined

#ifdef THREAD_EXITS_BEFORE_DETACH_CALL
long long int delay_thread = 10 ;
long long int delay_main =   1000000 ;
#else
long long int delay_thread = 1000000 ;
long long int delay_main =   10 ;
#endif

int Thread( void* d )
{
( void )d ;

long long int i = 0 ;
while( i++ < delay_thread )
thrd_yield() ;

printf("thread done\n") ;
return 0 ;
}


int main( void )
{

thrd_t t ;

int v = thrd_create( &t , Thread , NULL ) ;
assert( v == thrd_success ) ;

long long int i = 0 ;
while( i++ < delay_main )
thrd_yield() ;

printf("detach now\n") ;
int b= thrd_detach( t ) ;
assert( b == thrd_success ) ;


printf("done\n") ;
while( 1 )
{
thrd_yield() ;//leave the main thread running
}

return 0 ;
}

If you have a different compiler that supports c11 and threads.h please try the crash version of the above code example.

I using windows7 32 bit version, Pelles 7.00.355, and optimizations disabled.
« Last Edit: February 14, 2014, 07:40:23 PM by neo313 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Bug in implementation of thrd_detach(), of desired behaviour?
« Reply #1 on: February 15, 2014, 09:56:57 AM »
If you have a different compiler that supports c11 and threads.h please try the crash version of the above code example.
In ms vcpp e 2013 code works.
« Last Edit: February 15, 2014, 10:06:06 AM by timovjl »
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Bug in implementation of thrd_detach(), of desired behaviour?
« Reply #2 on: February 15, 2014, 01:31:03 PM »
As far I can found the system crashes when thread exits trying to lock the thread with a null pointer.
Maybe this is due to the ExitThread routine to call again thread_detach or something missing on the thread storage  >:(
No crash happens if the thread is not running because the code simply skips over.
Anyway it seems a bug!  :'(
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

neo313

  • Guest
Re: Bug in implementation of thrd_detach(), of desired behaviour?
« Reply #3 on: February 15, 2014, 04:04:53 PM »
If you have a different compiler that supports c11 and threads.h please try the crash version of the above code example.
In ms vcpp e 2013 code works.
Thank you,
Since when does visual studio 2013 support c11? I though they only got c99 in.


Quote
Anyway it seems a bug!  :'(
Yeah, that is too bad, I found a standalone pthread.h version that works on windows, on guthub. There is also a threads.h implementation made on top of pthread.h.
I wonder if there is a standalone threads.h version.

« Last Edit: February 15, 2014, 04:55:28 PM by neo313 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Bug in implementation of thrd_detach(), of desired behaviour?
« Reply #4 on: February 15, 2014, 05:22:12 PM »
Since when does visual studio 2013 support c11? I though they only got c99 in.
It doesn't support C11, but C++11, which incidentally include thread support in a C11 similar way.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide