threadidafterfork.c
来自「cygwin, 著名的在win32下模拟unix操作系统的东东」· C语言 代码 · 共 50 行
C
50 行
#include <stdio.h>#include <unistd.h>#include <sys/wait.h>#include <pthread.h>static void * TestThread ( void * );int main (void){ pthread_t t; pthread_create (&t, NULL, TestThread, NULL); pthread_join (t, NULL); return 0;}static void * TestThread ( void *not_used ){ pthread_t iAm = pthread_self(); int status; switch (fork ()) { case -1: exit(1); case 0: if (iAm != pthread_self()) exit (1); else exit (0); break; default: wait (&status); if (status != 0) exit (1); } exit(0);}/*The forked child will not get the same thread handle as its parent, itwill get the thread handle from the main thread instead. The child willnot terminate because the threadcount is still 2 after the fork (it isset to 1 in MTinterface::Init and then set back to 2 after the childsmemory gets overwritten by the parent).concept test by Thomas Pfaff <tpfaff@gmx.net>scritable test by Robert Collins <rbtcollins@hotmail.com>*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?