threadid.c

来自「unix环境高级编程(第二版)源代码」· C语言 代码 · 共 44 行

C
44
字号
#include "apue.h"#include <pthread.h>pthread_t ntid;voidprintids(const char *s){	pid_t		pid;	pthread_t	tid;	pid = getpid();	tid = pthread_self();	printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,	  (unsigned int)tid, (unsigned int)tid);}void *thr_fn(void *arg){	printids("new thread: ");	printf("in thr_fn new thread is %d\n",ntid);//	return((void *)5);	pthread_exit((void *)2);}intmain(void){	int		err;	void *rval_ptr;	err = pthread_create(&ntid, NULL, thr_fn, NULL);	printf("err is %d\n",err);	if (err != 0)		printf("can't create thread: %s\n", strerror(err));	printids("main thread:");	printf ("main new thread is %d\n",ntid);	err = pthread_join(ntid, &rval_ptr);	printf("rcal_ptr is %d\n",(int)rval_ptr);	sleep(100);	exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?