lhmouse/mcfgthread

Calling `thrd_exit()` in the primary thread results in a crash.

lhmouse opened this issue · 2 comments

The follow program illustrates what happens:

E:\Desktop>cat test.c
#include <mcfgthread/c11thread.h>

int main(){
        thrd_exit(1);
}

E:\Desktop>gcc test.c

E:\Desktop>a
The program has asked MCF CRT to terminate it abnormally. Please contact the author for detailed information.

Error description:
Assertion failed!

Expression: pControl
File: ../mcfgthread/src/env/_mopthread.c
Line: 189
Desc: Calling thread of __MCFCRT_MopthreadExit() was not created using __MCFCRT_MopthreadCreate().

Click OK to terminate the program; click Cancel to debug the program.

Note this crash happens because the primary thread is not created by mcfgthread. Consequently, although it seems possible to join with the thread, it is impossible to obtain its exit code, nor can we obtain the exit code of a thread created by __gthread_create(), using thrd_join().

There is another standard conformance issue:

ISO/IEC WG14 N1570

7.26.5.5 The thrd_exit function

3 The program shall terminate normally after the last thread has been terminated. The
behavior shall be as if the program called the exit function with the status
EXIT_SUCCESS at thread termination time.

This is not true on Windows. If you call ExitThread(code) from the last thread, the process terminates with that code. We need to work around this.

Update on 2017-06-02

A thread created with __gthread_create() or thrd_create() always exits with a code of zero, regardless of its procedure's return value. This is because it is based on the mopthread (the Mother Of PTHREAD) abstraction layer which makes no use of the native thread exit code. A program is not suffering from this issue if it does not call the ExitThread() function directly.

Fixed in 5c3f179.