メインスレッドの detachstate

pthreadではデフォルトでは他のスレッドからのjoinが可能なPTHREAD_CREATE_JOINABLEでスレッドを生成する。
しかし、メインスレッドがどうかっていうのは決まって無いんだかなんだか良くわからない。てなことを少し思って(まあ需要は無いだろうけれども)メインスレッドを待ち合わせしようとしたらどうなるか?で調べられるなあ、とか当たり前のことやってみた。

テストプログラム

#include <pthread.h>
#include <stdio.h>

pthread_t main_thread;

void *foo(void *args)
{
        int ret;

        printf("child:: let's join!!\n");
        pthread_join(main_thread, (void *)&ret);
        printf("child:: joined %d\n", ret);
        pthread_exit((void*) 10);
}

main()
{
        pthread_t tid;
        main_thread = pthread_self();
        pthread_create(&tid, NULL, foo, NULL);
        sleep(1);

        pthread_exit((void*)0);
}

Linux

Linuxは普通に default = JOINABLEになってるっぽい。

kuro@sawshark:~$ uname -a
Linux sawshark 2.6.24-24-generic #1 SMP Tue Aug 18 16:22:17 UTC 2009 x86_64 GNU/Linux
kuro@sawshark:~$ ./mainjoin
child:: let's join!!
child:: joined 0

まあ、問題なし。

MacOSX

Macだと、、おぉ、SEGVで死んだ。ぅわーい(喜ぶな)。どこで死んでいるかを調べてみると、、

sardine% gdb mainjoin
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done

(gdb) run
Starting program: /Users/tkuro/mainjoin 
Reading symbols for shared libraries +. done
child:: let's join!!

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
[Switching to process 18487]
0x00007fff802ee694 in pthread_join ()

おお、なんぞ DETACHEDっぽいぞなもし。というか無駄に64-bitなんだなーとか無意味に感心してみたりしたのは内緒。
というわけで多分 MacOSXはメインスレッドは DETACHEDらしい。Mach thread のデフォルトがそうなんだろうか?
とか思って調べようとして時間が無くなった<イマココ
後で調べよう。